-
Notifications
You must be signed in to change notification settings - Fork 657
/
data_source_ibm_cr_namespaces.go
117 lines (107 loc) · 3.63 KB
/
data_source_ibm_cr_namespaces.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
// Copyright IBM Corp. 2021 All Rights Reserved.
// Licensed under the Mozilla Public License v2.0
package registry
import (
"context"
"fmt"
"time"
"github.com/IBM-Cloud/terraform-provider-ibm/ibm/conns"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/IBM/container-registry-go-sdk/containerregistryv1"
)
func DataIBMContainerRegistryNamespaces() *schema.Resource {
return &schema.Resource{
ReadContext: dataIBMContainerRegistryNamespacesRead,
Schema: map[string]*schema.Schema{
"namespaces": {
Type: schema.TypeList,
Computed: true,
Description: "Container Registry Namespaces",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Computed: true,
Description: "Container Registry Namespace name",
},
"resource_group_id": {
Type: schema.TypeString,
Computed: true,
Description: "Resource Group to which namespace has to be assigned",
},
"crn": {
Type: schema.TypeString,
Computed: true,
Description: "CRN of the Namespace",
},
"created_date": {
Type: schema.TypeString,
Computed: true,
Description: "Created Date",
},
"updated_date": {
Type: schema.TypeString,
Computed: true,
Description: "Updated Date",
},
"resource_created_date": {
Type: schema.TypeString,
Computed: true,
Description: "When the namespace was assigned to a resource group.",
},
"account": {
Type: schema.TypeString,
Computed: true,
Description: "The IBM Cloud account that owns the namespace.",
},
// DEPRECATED FIELDS TO BE REMOVED IN FUTURE
"created_on": {
Type: schema.TypeString,
Computed: true,
Description: "Created Date",
Deprecated: "This field is deprecated",
},
"updated_on": {
Type: schema.TypeString,
Computed: true,
Description: "Updated Date",
Deprecated: "This field is deprecated",
},
},
},
},
},
}
}
func dataIBMContainerRegistryNamespacesRead(context context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
containerRegistryClient, err := meta.(conns.ClientSession).ContainerRegistryV1()
if err != nil {
return diag.FromErr(err)
}
listNamespaceDetailsOptions := &containerregistryv1.ListNamespaceDetailsOptions{}
namespaceDetailsList, _, err := containerRegistryClient.ListNamespaceDetails(listNamespaceDetailsOptions)
if err != nil {
return diag.FromErr(err)
}
namespaces := []map[string]interface{}{}
for _, namespaceDetails := range namespaceDetailsList {
namespace := map[string]interface{}{}
namespace["name"] = namespaceDetails.Name
namespace["resource_group_id"] = namespaceDetails.ResourceGroup
namespace["crn"] = namespaceDetails.CRN
namespace["created_date"] = namespaceDetails.CreatedDate
namespace["updated_date"] = namespaceDetails.UpdatedDate
namespace["account"] = namespaceDetails.Account
namespace["resource_created_date"] = namespaceDetails.ResourceCreatedDate
// DEPRECATED FIELDS TO BE REMOVED IN FUTURE
namespace["created_on"] = namespaceDetails.CreatedDate
namespace["updated_on"] = namespaceDetails.UpdatedDate
namespaces = append(namespaces, namespace)
}
if err = d.Set("namespaces", namespaces); err != nil {
return diag.FromErr(fmt.Errorf("[ERROR] Error setting namespaces: %s", err))
}
d.SetId(time.Now().UTC().String())
return nil
}