|
| 1 | +package swr |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "strings" |
| 6 | + "time" |
| 7 | + |
| 8 | + "github.com/hashicorp/go-multierror" |
| 9 | + "github.com/hashicorp/go-uuid" |
| 10 | + "github.com/hashicorp/terraform-plugin-sdk/v2/diag" |
| 11 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" |
| 12 | + "github.com/opentelekomcloud/gophertelekomcloud/openstack/swr/v2/domains" |
| 13 | + "github.com/opentelekomcloud/terraform-provider-opentelekomcloud/opentelekomcloud/common/cfg" |
| 14 | + "github.com/opentelekomcloud/terraform-provider-opentelekomcloud/opentelekomcloud/common/fmterr" |
| 15 | +) |
| 16 | + |
| 17 | +func DataSourceSwrDomainsV2() *schema.Resource { |
| 18 | + return &schema.Resource{ |
| 19 | + ReadContext: dataSourceSwrDomainRead, |
| 20 | + |
| 21 | + Timeouts: &schema.ResourceTimeout{ |
| 22 | + Default: schema.DefaultTimeout(2 * time.Minute), |
| 23 | + }, |
| 24 | + |
| 25 | + Schema: map[string]*schema.Schema{ |
| 26 | + "organization": { |
| 27 | + Type: schema.TypeString, |
| 28 | + Required: true, |
| 29 | + }, |
| 30 | + "repository": { |
| 31 | + Type: schema.TypeString, |
| 32 | + Required: true, |
| 33 | + }, |
| 34 | + "access_domains": { |
| 35 | + Type: schema.TypeList, |
| 36 | + Computed: true, |
| 37 | + Elem: &schema.Resource{ |
| 38 | + Schema: map[string]*schema.Schema{ |
| 39 | + "access_domain": { |
| 40 | + Type: schema.TypeString, |
| 41 | + Computed: true, |
| 42 | + }, |
| 43 | + "permission": { |
| 44 | + Type: schema.TypeString, |
| 45 | + Computed: true, |
| 46 | + }, |
| 47 | + "deadline": { |
| 48 | + Type: schema.TypeString, |
| 49 | + Computed: true, |
| 50 | + }, |
| 51 | + "description": { |
| 52 | + Type: schema.TypeString, |
| 53 | + Computed: true, |
| 54 | + }, |
| 55 | + "creator_id": { |
| 56 | + Type: schema.TypeString, |
| 57 | + Computed: true, |
| 58 | + }, |
| 59 | + "creator_name": { |
| 60 | + Type: schema.TypeString, |
| 61 | + Computed: true, |
| 62 | + }, |
| 63 | + "created": { |
| 64 | + Type: schema.TypeString, |
| 65 | + Computed: true, |
| 66 | + }, |
| 67 | + "updated": { |
| 68 | + Type: schema.TypeString, |
| 69 | + Computed: true, |
| 70 | + }, |
| 71 | + "status": { |
| 72 | + Type: schema.TypeBool, |
| 73 | + Computed: true, |
| 74 | + }, |
| 75 | + }, |
| 76 | + }, |
| 77 | + }, |
| 78 | + }, |
| 79 | + } |
| 80 | +} |
| 81 | + |
| 82 | +func dataSourceSwrDomainsRead(_ context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { |
| 83 | + config := meta.(*cfg.Config) |
| 84 | + client, err := config.SwrV2Client(config.GetRegion(d)) |
| 85 | + if err != nil { |
| 86 | + return fmterr.Errorf(ClientError, err) |
| 87 | + } |
| 88 | + |
| 89 | + opts := domains.ListOpts{ |
| 90 | + Namespace: d.Get("organization").(string), |
| 91 | + Repository: repository(d.Get("repository").(string)), |
| 92 | + } |
| 93 | + accessDomains, err := domains.List(client, opts) |
| 94 | + if err != nil { |
| 95 | + return fmterr.Errorf("error reading domain: %w", err) |
| 96 | + } |
| 97 | + id, err := uuid.GenerateUUID() |
| 98 | + if err != nil { |
| 99 | + return diag.Errorf("unable to generate ID: %s", err) |
| 100 | + } |
| 101 | + d.SetId(id) |
| 102 | + |
| 103 | + mErr := multierror.Append( |
| 104 | + d.Set("access_domains", setAccessDomains(accessDomains)), |
| 105 | + ) |
| 106 | + if err := mErr.ErrorOrNil(); err != nil { |
| 107 | + return fmterr.Errorf("error setting resource fields: %w", err) |
| 108 | + } |
| 109 | + |
| 110 | + return nil |
| 111 | +} |
| 112 | + |
| 113 | +func setAccessDomains(domainsInResp []domains.AccessDomain) []map[string]interface{} { |
| 114 | + var accessDomains []map[string]interface{} |
| 115 | + for _, accessDomainInResp := range domainsInResp { |
| 116 | + accessDomain := map[string]interface{}{ |
| 117 | + "access_domain": strings.ToUpper(accessDomainInResp.AccessDomain), |
| 118 | + "description": accessDomainInResp.Description, |
| 119 | + "permission": accessDomainInResp.Permit, |
| 120 | + "deadline": accessDomainInResp.Deadline, |
| 121 | + "created": accessDomainInResp.Created, |
| 122 | + "updated": accessDomainInResp.Updated, |
| 123 | + "creator_id": accessDomainInResp.CreatorID, |
| 124 | + "creator_name": accessDomainInResp.CreatorName, |
| 125 | + "status": accessDomainInResp.Status, |
| 126 | + } |
| 127 | + accessDomains = append(accessDomains, accessDomain) |
| 128 | + } |
| 129 | + return accessDomains |
| 130 | +} |
0 commit comments