forked from nutanix/terraform-provider-nutanix
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdata_source_nutanix_karbon_private_registry.go
116 lines (101 loc) · 3.21 KB
/
data_source_nutanix_karbon_private_registry.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
package nutanix
import (
"fmt"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/terraform-providers/terraform-provider-nutanix/client/karbon"
"github.com/terraform-providers/terraform-provider-nutanix/utils"
)
func dataSourceNutanixKarbonPrivateRegistry() *schema.Resource {
return &schema.Resource{
Read: dataSourceNutanixKarbonPrivateRegistryRead,
SchemaVersion: 1,
Schema: KarbonPrivateRegistryDataSourceMap(),
}
}
func dataSourceNutanixKarbonPrivateRegistryRead(d *schema.ResourceData, meta interface{}) error {
// Get client connection
conn := meta.(*Client).KarbonAPI
setTimeout(meta)
// Make request to the API
karbonPrivateRegistryID, iok := d.GetOk("private_registry_id")
karbonPrivateRegistryName, nok := d.GetOk("private_registry_name")
if !iok && !nok {
return fmt.Errorf("please provide one of private_registry_id or private_registry_name attributes")
}
var err error
var resp *karbon.PrivateRegistryResponse
if iok {
resp, err = findPrivateRegistryByUUID(conn, karbonPrivateRegistryID.(string))
} else {
resp, err = findPrivateRegistryByName(conn, karbonPrivateRegistryName.(string))
}
if err != nil {
d.SetId("")
return err
}
uuid := utils.StringValue(resp.UUID)
if err := d.Set("name", utils.StringValue(resp.Name)); err != nil {
return fmt.Errorf("error occurred while setting name: %s", err)
}
if err := d.Set("endpoint", utils.StringValue(resp.Endpoint)); err != nil {
return fmt.Errorf("error occurred while setting endpoint: %s", err)
}
if err := d.Set("uuid", uuid); err != nil {
return fmt.Errorf("error occurred while setting endpoint: %s", err)
}
d.SetId(uuid)
return nil
}
func findPrivateRegistryByName(conn *karbon.Client, name string) (*karbon.PrivateRegistryResponse, error) {
return conn.PrivateRegistry.GetKarbonPrivateRegistry(name)
}
func findPrivateRegistryByUUID(conn *karbon.Client, uuid string) (*karbon.PrivateRegistryResponse, error) {
resp, err := conn.PrivateRegistry.ListKarbonPrivateRegistries()
if err != nil {
return nil, err
}
found := make([]*karbon.PrivateRegistryResponse, 0)
for _, v := range *resp {
reg := v
if *v.UUID == uuid {
found = append(found, ®)
}
}
if len(found) > 1 {
return nil, fmt.Errorf("your query returned more than one result. Please use private_registry_name argument instead")
}
if len(found) == 0 {
return nil, fmt.Errorf("private registry with the given uuid not found")
}
return found[0], nil
}
func KarbonPrivateRegistryDataSourceMap() map[string]*schema.Schema {
kcsm := KarbonPrivateRegistryElementDataSourceMap()
kcsm["private_registry_id"] = &schema.Schema{
Type: schema.TypeString,
Optional: true,
ConflictsWith: []string{"private_registry_name"},
}
kcsm["private_registry_name"] = &schema.Schema{
Type: schema.TypeString,
Optional: true,
ConflictsWith: []string{"private_registry_id"},
}
return kcsm
}
func KarbonPrivateRegistryElementDataSourceMap() map[string]*schema.Schema {
return map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Computed: true,
},
"endpoint": {
Type: schema.TypeString,
Computed: true,
},
"uuid": {
Type: schema.TypeString,
Computed: true,
},
}
}