/
data_source_flexibleengine_compute_bms_keypair_v2.go
77 lines (60 loc) · 1.68 KB
/
data_source_flexibleengine_compute_bms_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
66
67
68
69
70
71
72
73
74
75
76
77
package flexibleengine
import (
"fmt"
"log"
"github.com/chnsz/golangsdk/openstack/bms/v2/keypairs"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
func dataSourceBMSKeyPairV2() *schema.Resource {
return &schema.Resource{
Read: dataSourceBMSKeyPairV2Read,
Schema: map[string]*schema.Schema{
"region": {
Type: schema.TypeString,
Optional: true,
Computed: true,
},
"name": {
Type: schema.TypeString,
Required: true,
},
"public_key": {
Type: schema.TypeString,
Computed: true,
},
"fingerprint": {
Type: schema.TypeString,
Computed: true,
},
},
}
}
func dataSourceBMSKeyPairV2Read(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
bmsClient, err := config.ComputeV2Client(GetRegion(d, config))
if err != nil {
return fmt.Errorf("Error creating Flexibleengine bms client: %s", err)
}
listOpts := keypairs.ListOpts{
Name: d.Get("name").(string),
}
refinedKeypairs, err := keypairs.List(bmsClient, listOpts)
if err != nil {
return fmt.Errorf("Unable to retrieve keypairs: %s", err)
}
if len(refinedKeypairs) < 1 {
return fmt.Errorf("Your query returned no results. " +
"Please change your search criteria and try again.")
}
if len(refinedKeypairs) > 1 {
return fmt.Errorf("Your query returned more than one result." +
" Please try a more specific search criteria")
}
Keypairs := refinedKeypairs[0]
log.Printf("[INFO] Retrieved Keypairs using given filter %s: %+v", Keypairs.Name, Keypairs)
d.SetId(Keypairs.Name)
d.Set("public_key", Keypairs.PublicKey)
d.Set("fingerprint", Keypairs.Fingerprint)
d.Set("region", GetRegion(d, config))
return nil
}