|
| 1 | +package cci |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + |
| 7 | + "github.com/hashicorp/go-multierror" |
| 8 | + "github.com/hashicorp/go-uuid" |
| 9 | + "github.com/hashicorp/terraform-plugin-sdk/v2/diag" |
| 10 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" |
| 11 | + |
| 12 | + "github.com/chnsz/golangsdk" |
| 13 | + |
| 14 | + "github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/config" |
| 15 | + "github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/utils" |
| 16 | +) |
| 17 | + |
| 18 | +// @API CCI GET /apis/cci/v2/namespaces |
| 19 | +// @API CCI GET /apis/cci/v2/namespaces/{name} |
| 20 | +func DataSourceV2Namespaces() *schema.Resource { |
| 21 | + return &schema.Resource{ |
| 22 | + ReadContext: dataSourceV2NamespacesRead, |
| 23 | + |
| 24 | + Schema: map[string]*schema.Schema{ |
| 25 | + "region": { |
| 26 | + Type: schema.TypeString, |
| 27 | + Optional: true, |
| 28 | + Computed: true, |
| 29 | + }, |
| 30 | + "name": { |
| 31 | + Type: schema.TypeString, |
| 32 | + Optional: true, |
| 33 | + }, |
| 34 | + "namespaces": { |
| 35 | + Type: schema.TypeList, |
| 36 | + Computed: true, |
| 37 | + Elem: &schema.Resource{ |
| 38 | + Schema: map[string]*schema.Schema{ |
| 39 | + "name": { |
| 40 | + Type: schema.TypeString, |
| 41 | + Computed: true, |
| 42 | + Description: `The name of the namespace.`, |
| 43 | + }, |
| 44 | + "api_version": { |
| 45 | + Type: schema.TypeString, |
| 46 | + Computed: true, |
| 47 | + Description: `The API version of the namespace.`, |
| 48 | + }, |
| 49 | + "kind": { |
| 50 | + Type: schema.TypeString, |
| 51 | + Computed: true, |
| 52 | + Description: `The kind of the namespace.`, |
| 53 | + }, |
| 54 | + "annotations": { |
| 55 | + Type: schema.TypeMap, |
| 56 | + Computed: true, |
| 57 | + Elem: &schema.Schema{Type: schema.TypeString}, |
| 58 | + Description: `The annotations of the namespace.`, |
| 59 | + }, |
| 60 | + "labels": { |
| 61 | + Type: schema.TypeMap, |
| 62 | + Computed: true, |
| 63 | + Elem: &schema.Schema{Type: schema.TypeString}, |
| 64 | + Description: `The labels of the namespace.`, |
| 65 | + }, |
| 66 | + "creation_timestamp": { |
| 67 | + Type: schema.TypeString, |
| 68 | + Computed: true, |
| 69 | + Description: `The creation timestamp of the namespace.`, |
| 70 | + }, |
| 71 | + "finalizers": { |
| 72 | + Type: schema.TypeList, |
| 73 | + Elem: &schema.Schema{Type: schema.TypeString}, |
| 74 | + Computed: true, |
| 75 | + Description: `The finalizers of the namespace.`, |
| 76 | + }, |
| 77 | + "resource_version": { |
| 78 | + Type: schema.TypeString, |
| 79 | + Computed: true, |
| 80 | + Description: `The resource version of the namespace.`, |
| 81 | + }, |
| 82 | + "uid": { |
| 83 | + Type: schema.TypeString, |
| 84 | + Computed: true, |
| 85 | + Description: `The uid of the namespace.`, |
| 86 | + }, |
| 87 | + "status": { |
| 88 | + Type: schema.TypeString, |
| 89 | + Computed: true, |
| 90 | + Description: `The status of the namespace.`, |
| 91 | + }, |
| 92 | + }, |
| 93 | + }, |
| 94 | + }, |
| 95 | + }, |
| 96 | + } |
| 97 | +} |
| 98 | + |
| 99 | +func dataSourceV2NamespacesRead(_ context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { |
| 100 | + cfg := meta.(*config.Config) |
| 101 | + region := cfg.GetRegion(d) |
| 102 | + client, err := cfg.NewServiceClient("cci", region) |
| 103 | + if err != nil { |
| 104 | + return diag.Errorf("error creating CCI client: %s", err) |
| 105 | + } |
| 106 | + |
| 107 | + nsList := make([]interface{}, 0) |
| 108 | + if ns, ok := d.GetOk("name"); ok { |
| 109 | + resp, err := GetNamespaceDetail(client, ns.(string)) |
| 110 | + if err != nil { |
| 111 | + return diag.Errorf("error getting the namespace (%s) from the server: %s", ns.(string), err) |
| 112 | + } |
| 113 | + nsList = append(nsList, resp) |
| 114 | + } else { |
| 115 | + resp, err := listNamespaces(client) |
| 116 | + if err != nil { |
| 117 | + return diag.Errorf("error finding the namespace list from the server: %s", err) |
| 118 | + } |
| 119 | + nsList = utils.PathSearch("items", resp, make([]interface{}, 0)).([]interface{}) |
| 120 | + } |
| 121 | + |
| 122 | + uuid, err := uuid.GenerateUUID() |
| 123 | + if err != nil { |
| 124 | + return diag.Errorf("unable to generate ID: %s", err) |
| 125 | + } |
| 126 | + d.SetId(uuid) |
| 127 | + |
| 128 | + mErr := multierror.Append(nil, |
| 129 | + d.Set("region", region), |
| 130 | + d.Set("namespaces", flattenNamespace(nsList)), |
| 131 | + ) |
| 132 | + |
| 133 | + return diag.FromErr(mErr.ErrorOrNil()) |
| 134 | +} |
| 135 | + |
| 136 | +func flattenNamespace(nsList []interface{}) []interface{} { |
| 137 | + if len(nsList) == 0 { |
| 138 | + return nil |
| 139 | + } |
| 140 | + |
| 141 | + rst := make([]interface{}, 0, len(nsList)) |
| 142 | + for _, v := range nsList { |
| 143 | + rst = append(rst, map[string]interface{}{ |
| 144 | + "name": utils.PathSearch("metadata.name", v, nil), |
| 145 | + "api_version": utils.PathSearch("apiVersion", v, nil), |
| 146 | + "kind": utils.PathSearch("kind", v, nil), |
| 147 | + "annotations": utils.PathSearch("metadata.annotations", v, nil), |
| 148 | + "labels": utils.PathSearch("metadata.labels", v, nil), |
| 149 | + "creation_timestamp": utils.PathSearch("metadata.creationTimestamp", v, nil), |
| 150 | + "resource_version": utils.PathSearch("metadata.resourceVersion", v, nil), |
| 151 | + "uid": utils.PathSearch("metadata.uid", v, nil), |
| 152 | + "finalizers": utils.PathSearch("spec.finalizers", v, nil), |
| 153 | + "status": utils.PathSearch("status.phase", v, nil), |
| 154 | + }) |
| 155 | + } |
| 156 | + return rst |
| 157 | +} |
| 158 | + |
| 159 | +func listNamespaces(client *golangsdk.ServiceClient) (interface{}, error) { |
| 160 | + listNamespaceHttpUrl := "apis/cci/v2/namespaces" |
| 161 | + listNamespacePath := client.Endpoint + listNamespaceHttpUrl |
| 162 | + listNamespaceOpt := golangsdk.RequestOpts{ |
| 163 | + KeepResponseBody: true, |
| 164 | + MoreHeaders: map[string]string{"Content-Type": "application/json"}, |
| 165 | + } |
| 166 | + |
| 167 | + listNamespacesResp, err := client.Request("GET", listNamespacePath, &listNamespaceOpt) |
| 168 | + if err != nil { |
| 169 | + return nil, fmt.Errorf("error querying CCI namespaces: %s", err) |
| 170 | + } |
| 171 | + |
| 172 | + return utils.FlattenResponse(listNamespacesResp) |
| 173 | +} |
0 commit comments