-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdata_source_openstack_compute_keypair_v2.go
65 lines (51 loc) · 1.48 KB
/
data_source_openstack_compute_keypair_v2.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
package openstack
import (
"context"
"log"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/gophercloud/gophercloud/openstack/compute/v2/extensions/keypairs"
)
func dataSourceComputeKeypairV2() *schema.Resource {
return &schema.Resource{
ReadContext: dataSourceComputeKeypairV2Read,
Schema: map[string]*schema.Schema{
"region": {
Type: schema.TypeString,
Optional: true,
Computed: true,
},
"name": {
Type: schema.TypeString,
Required: true,
},
// computed-only
"fingerprint": {
Type: schema.TypeString,
Computed: true,
},
"public_key": {
Type: schema.TypeString,
Computed: true,
},
},
}
}
func dataSourceComputeKeypairV2Read(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
config := meta.(*Config)
computeClient, err := config.ComputeV2Client(GetRegion(d, config))
if err != nil {
return diag.Errorf("Error creating OpenStack compute client: %s", err)
}
name := d.Get("name").(string)
kp, err := keypairs.Get(computeClient, name, keypairs.GetOpts{}).Extract()
if err != nil {
return diag.Errorf("Error retrieving openstack_compute_keypair_v2 %s: %s", name, err)
}
d.SetId(name)
log.Printf("[DEBUG] Retrieved openstack_compute_keypair_v2 %s: %#v", d.Id(), kp)
d.Set("fingerprint", kp.Fingerprint)
d.Set("public_key", kp.PublicKey)
d.Set("region", GetRegion(d, config))
return nil
}