|
| 1 | +// Generated by PMS #612 |
| 2 | +package organizations |
| 3 | + |
| 4 | +import ( |
| 5 | + "context" |
| 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 | + "github.com/tidwall/gjson" |
| 12 | + |
| 13 | + "github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/config" |
| 14 | + "github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/helper/httphelper" |
| 15 | + "github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/helper/schemas" |
| 16 | + "github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/utils" |
| 17 | +) |
| 18 | + |
| 19 | +func DataSourceOrganizationsCloseAccountStatus() *schema.Resource { |
| 20 | + return &schema.Resource{ |
| 21 | + ReadContext: dataSourceOrganizationsCloseAccountStatusRead, |
| 22 | + |
| 23 | + Schema: map[string]*schema.Schema{ |
| 24 | + "states": { |
| 25 | + Type: schema.TypeList, |
| 26 | + Optional: true, |
| 27 | + Elem: &schema.Schema{Type: schema.TypeString}, |
| 28 | + Description: `Specifies the list of one or more states that you want to include in the response.`, |
| 29 | + }, |
| 30 | + "close_account_statuses": { |
| 31 | + Type: schema.TypeList, |
| 32 | + Computed: true, |
| 33 | + Description: `Indicates the list of close account statuses.`, |
| 34 | + Elem: &schema.Resource{ |
| 35 | + Schema: map[string]*schema.Schema{ |
| 36 | + "account_id": { |
| 37 | + Type: schema.TypeString, |
| 38 | + Computed: true, |
| 39 | + Description: `Indicates the ID of an account.`, |
| 40 | + }, |
| 41 | + "state": { |
| 42 | + Type: schema.TypeString, |
| 43 | + Computed: true, |
| 44 | + Description: `Indicates the Status of the close account request.`, |
| 45 | + }, |
| 46 | + "organization_id": { |
| 47 | + Type: schema.TypeString, |
| 48 | + Computed: true, |
| 49 | + Description: `Indicates the ID of an organization.`, |
| 50 | + }, |
| 51 | + "failure_reason": { |
| 52 | + Type: schema.TypeString, |
| 53 | + Computed: true, |
| 54 | + Description: `Indicates the reason for a request failure.`, |
| 55 | + }, |
| 56 | + "created_at": { |
| 57 | + Type: schema.TypeString, |
| 58 | + Computed: true, |
| 59 | + Description: `Indicates the date and time when the close account request was made.`, |
| 60 | + }, |
| 61 | + "updated_at": { |
| 62 | + Type: schema.TypeString, |
| 63 | + Computed: true, |
| 64 | + Description: `Indicates the date and time when the status of close account request was updated.`, |
| 65 | + }, |
| 66 | + }, |
| 67 | + }, |
| 68 | + }, |
| 69 | + }, |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +type CloseAccountStatusDSWrapper struct { |
| 74 | + *schemas.ResourceDataWrapper |
| 75 | + Config *config.Config |
| 76 | +} |
| 77 | + |
| 78 | +func newCloseAccountStatusDSWrapper(d *schema.ResourceData, meta interface{}) *CloseAccountStatusDSWrapper { |
| 79 | + return &CloseAccountStatusDSWrapper{ |
| 80 | + ResourceDataWrapper: schemas.NewSchemaWrapper(d), |
| 81 | + Config: meta.(*config.Config), |
| 82 | + } |
| 83 | +} |
| 84 | + |
| 85 | +func dataSourceOrganizationsCloseAccountStatusRead(_ context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { |
| 86 | + wrapper := newCloseAccountStatusDSWrapper(d, meta) |
| 87 | + lisCloAccStaRst, err := wrapper.ListCloseAccountStatuses() |
| 88 | + if err != nil { |
| 89 | + return diag.FromErr(err) |
| 90 | + } |
| 91 | + |
| 92 | + id, err := uuid.GenerateUUID() |
| 93 | + if err != nil { |
| 94 | + return diag.FromErr(err) |
| 95 | + } |
| 96 | + d.SetId(id) |
| 97 | + |
| 98 | + err = wrapper.listCloseAccountStatusesToSchema(lisCloAccStaRst) |
| 99 | + if err != nil { |
| 100 | + return diag.FromErr(err) |
| 101 | + } |
| 102 | + |
| 103 | + return nil |
| 104 | +} |
| 105 | + |
| 106 | +// @API Organizations GET /v1/organizations/close-account-status |
| 107 | +func (w *CloseAccountStatusDSWrapper) ListCloseAccountStatuses() (*gjson.Result, error) { |
| 108 | + client, err := w.NewClient(w.Config, "organizations") |
| 109 | + if err != nil { |
| 110 | + return nil, err |
| 111 | + } |
| 112 | + |
| 113 | + uri := "/v1/organizations/close-account-status" |
| 114 | + params := map[string]any{ |
| 115 | + "states": w.ListToArray("states"), |
| 116 | + } |
| 117 | + params = utils.RemoveNil(params) |
| 118 | + return httphelper.New(client). |
| 119 | + Method("GET"). |
| 120 | + URI(uri). |
| 121 | + Query(params). |
| 122 | + Request(). |
| 123 | + Result() |
| 124 | +} |
| 125 | + |
| 126 | +func (w *CloseAccountStatusDSWrapper) listCloseAccountStatusesToSchema(body *gjson.Result) error { |
| 127 | + d := w.ResourceData |
| 128 | + mErr := multierror.Append(nil, |
| 129 | + d.Set("close_account_statuses", schemas.SliceToList(body.Get("close_account_statuses"), |
| 130 | + func(cloAccStatuses gjson.Result) any { |
| 131 | + return map[string]any{ |
| 132 | + "account_id": cloAccStatuses.Get("account_id").Value(), |
| 133 | + "state": cloAccStatuses.Get("state").Value(), |
| 134 | + "organization_id": cloAccStatuses.Get("organization_id").Value(), |
| 135 | + "failure_reason": cloAccStatuses.Get("failure_reason").Value(), |
| 136 | + "created_at": cloAccStatuses.Get("created_at").Value(), |
| 137 | + "updated_at": cloAccStatuses.Get("updated_at").Value(), |
| 138 | + } |
| 139 | + }, |
| 140 | + )), |
| 141 | + ) |
| 142 | + return mErr.ErrorOrNil() |
| 143 | +} |
0 commit comments