-
Notifications
You must be signed in to change notification settings - Fork 221
/
data_source_rancher2_cloud_credential.go
67 lines (56 loc) · 1.37 KB
/
data_source_rancher2_cloud_credential.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
package rancher2
import (
"fmt"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
)
func dataSourceRancher2CloudCredential() *schema.Resource {
return &schema.Resource{
Read: dataSourceRancher2CloudCredentialRead,
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
},
"annotations": {
Type: schema.TypeMap,
Computed: true,
},
"labels": {
Type: schema.TypeMap,
Computed: true,
},
},
}
}
func dataSourceRancher2CloudCredentialRead(d *schema.ResourceData, meta interface{}) error {
client, err := meta.(*Config).ManagementClient()
if err != nil {
return err
}
name := d.Get("name").(string)
filters := map[string]interface{}{"name": name}
listOpts := NewListOpts(filters)
credentials, err := client.CloudCredential.List(listOpts)
if err != nil {
return err
}
count := len(credentials.Data)
if count <= 0 {
return fmt.Errorf("[ERROR] catalog with name \"%s\" not found", name)
}
if count > 1 {
return fmt.Errorf("[ERROR] found %d catalogs with name \"%s\"", count, name)
}
credential := credentials.Data[0]
d.SetId(credential.ID)
d.Set("name", credential.Name)
err = d.Set("annotations", toMapInterface(credential.Annotations))
if err != nil {
return err
}
err = d.Set("labels", toMapInterface(credential.Labels))
if err != nil {
return err
}
return nil
}