-
Notifications
You must be signed in to change notification settings - Fork 0
/
01-create-project.tf
40 lines (34 loc) · 1.28 KB
/
01-create-project.tf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
locals {
timestamp = "${timestamp()}"
timestamp_sanitized = "${replace("${local.timestamp}", "/[-| |T|Z|:]/", "")}"
project_name_sanitized = "${replace("${var.project_name}", "/[ ]/", "_")}"
ssh_key_name = "${local.project_name_sanitized}-${local.timestamp_sanitized}-key"
}
resource "packet_project" "new_project" {
count = var.create_project ? 1 : 0
name = var.project_name
organization_id = var.organization_id
}
locals {
depends_on = [packet_project.new_project]
count = var.create_project ? 1 : 0
project_id = var.create_project ? packet_project.new_project[0].id : var.project_id
}
resource "tls_private_key" "ssh_key_pair" {
algorithm = "RSA"
rsa_bits = 4096
}
resource "packet_project_ssh_key" "ssh_pub_key" {
depends_on = [packet_project.new_project]
project_id = local.project_id
name = local.ssh_key_name
public_key = chomp(tls_private_key.ssh_key_pair.public_key_openssh)
}
resource "local_file" "project_private_key_pem" {
content = chomp(tls_private_key.ssh_key_pair.private_key_pem)
filename = pathexpand("~/.ssh/${local.ssh_key_name}")
file_permission = "0600"
provisioner "local-exec" {
command = "cp ~/.ssh/${local.ssh_key_name} ~/.ssh/${local.ssh_key_name}.bak"
}
}