forked from huaweicloud/terraform-provider-huaweicloud
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathresource_huaweicloud_compute_keypair.go
129 lines (110 loc) · 3.48 KB
/
resource_huaweicloud_compute_keypair.go
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
package huaweicloud
import (
"fmt"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/chnsz/golangsdk/openstack/compute/v2/extensions/keypairs"
"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/config"
"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/utils"
"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/utils/fmtp"
"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/utils/logp"
)
// @API ECS DELETE /v2.1/{project_id}/os-keypairs/{name}
// @API ECS GET /v2.1/{project_id}/os-keypairs/{name}
// @API ECS POST /v2.1/{project_id}/os-keypairs
func ResourceComputeKeypairV2() *schema.Resource {
return &schema.Resource{
Create: resourceComputeKeypairV2Create,
Read: resourceComputeKeypairV2Read,
Delete: resourceComputeKeypairV2Delete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
Schema: map[string]*schema.Schema{
"region": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
},
"name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"public_key": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
ConflictsWith: []string{"key_file"},
},
"key_file": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
},
},
}
}
func resourceComputeKeypairV2Create(d *schema.ResourceData, meta interface{}) error {
config := meta.(*config.Config)
computeClient, err := config.ComputeV2Client(GetRegion(d, config))
if err != nil {
return fmtp.Errorf("Error creating HuaweiCloud compute client: %s", err)
}
pk, isExist := d.GetOk("public_key")
createOpts := keypairs.CreateOpts{
Name: d.Get("name").(string),
PublicKey: pk.(string),
}
logp.Printf("[DEBUG] Create Options: %#v", createOpts)
kp, err := keypairs.Create(computeClient, createOpts).Extract()
if err != nil {
return fmtp.Errorf("Error creating HuaweiCloud keypair: %s", err)
}
d.SetId(kp.Name)
if !isExist {
fp := getKeyFilePath(d)
if err = utils.WriteToPemFile(fp, kp.PrivateKey); err != nil {
return fmtp.Errorf("Unable to generate private key: %s", err)
}
d.Set("key_file", fp)
}
return resourceComputeKeypairV2Read(d, meta)
}
func getKeyFilePath(d *schema.ResourceData) string {
if path, ok := d.GetOk("key_file"); ok {
return path.(string)
}
keypairName := d.Get("name").(string)
return fmt.Sprintf("%s.pem", keypairName)
}
func resourceComputeKeypairV2Read(d *schema.ResourceData, meta interface{}) error {
config := meta.(*config.Config)
computeClient, err := config.ComputeV2Client(GetRegion(d, config))
if err != nil {
return fmtp.Errorf("Error creating HuaweiCloud compute client: %s", err)
}
kp, err := keypairs.Get(computeClient, d.Id()).Extract()
if err != nil {
return CheckDeleted(d, err, "keypair")
}
d.Set("name", kp.Name)
d.Set("public_key", kp.PublicKey)
d.Set("region", GetRegion(d, config))
return nil
}
func resourceComputeKeypairV2Delete(d *schema.ResourceData, meta interface{}) error {
config := meta.(*config.Config)
computeClient, err := config.ComputeV2Client(GetRegion(d, config))
if err != nil {
return fmtp.Errorf("Error creating HuaweiCloud compute client: %s", err)
}
err = keypairs.Delete(computeClient, d.Id()).ExtractErr()
if err != nil {
return fmtp.Errorf("Error deleting HuaweiCloud keypair: %s", err)
}
d.SetId("")
return nil
}