Skip to content

Commit 054485b

Browse files
authored
feat(Organizations): add organizations close account status data source (#6699)
1 parent 945d4b1 commit 054485b

File tree

4 files changed

+246
-0
lines changed

4 files changed

+246
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
---
2+
subcategory: "Organizations"
3+
layout: "huaweicloud"
4+
page_title: "HuaweiCloud: huaweicloud_organizations_close_account_status"
5+
description: |-
6+
Use this data source to get the list of the account close requests for an organization.
7+
---
8+
9+
# huaweicloud_organizations_close_account_status
10+
11+
Use this data source to get the list of the account close requests for an organization.
12+
13+
## Example Usage
14+
15+
```hcl
16+
data "huaweicloud_organizations_close_account_status" "test" {}
17+
```
18+
19+
## Argument Reference
20+
21+
The following arguments are supported:
22+
23+
* `states` - (Optional, List) Specifies the list of one or more states that you want to include in the response.
24+
25+
## Attribute Reference
26+
27+
In addition to all arguments above, the following attributes are exported:
28+
29+
* `id` - The data source ID.
30+
31+
* `close_account_statuses` - Indicates the list of close account statuses.
32+
33+
The [close_account_statuses](#close_account_statuses_struct) structure is documented below.
34+
35+
<a name="close_account_statuses_struct"></a>
36+
The `close_account_statuses` block supports:
37+
38+
* `account_id` - Indicates the ID of an account.
39+
40+
* `state` - Indicates the Status of the close account request.
41+
42+
* `organization_id` - Indicates the ID of an organization.
43+
44+
* `failure_reason` - Indicates the reason for a request failure.
45+
46+
* `created_at` - Indicates the date and time when the close account request was made.
47+
48+
* `updated_at` - Indicates the date and time when the status of close account request was updated.

huaweicloud/provider.go

+1
Original file line numberDiff line numberDiff line change
@@ -1454,6 +1454,7 @@ func Provider() *schema.Provider {
14541454
"huaweicloud_organizations_resource_instances": organizations.DataSourceOrganizationsResourceInstances(),
14551455
"huaweicloud_organizations_quotas": organizations.DataSourceOrganizationsQuotas(),
14561456
"huaweicloud_organizations_create_account_status": organizations.DataSourceOrganizationsCreateAccountStatus(),
1457+
"huaweicloud_organizations_close_account_status": organizations.DataSourceOrganizationsCloseAccountStatus(),
14571458

14581459
// Deprecated ongoing (without DeprecationMessage), used by other providers
14591460
"huaweicloud_vpc_route": vpc.DataSourceVpcRouteV2(),
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package organizations
2+
3+
import (
4+
"testing"
5+
6+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
7+
8+
"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/services/acceptance"
9+
)
10+
11+
func TestAccDataSourceOrganizationsCloseAccountStatus_basic(t *testing.T) {
12+
dataSource := "data.huaweicloud_organizations_close_account_status.test"
13+
dc := acceptance.InitDataSourceCheck(dataSource)
14+
15+
resource.ParallelTest(t, resource.TestCase{
16+
PreCheck: func() {
17+
acceptance.TestAccPreCheck(t)
18+
},
19+
ProviderFactories: acceptance.TestAccProviderFactories,
20+
Steps: []resource.TestStep{
21+
{
22+
Config: testDataSourceOrganizationsCloseAccountStatus_basic(),
23+
Check: resource.ComposeTestCheckFunc(
24+
dc.CheckResourceExists(),
25+
resource.TestCheckResourceAttrSet(dataSource, "close_account_statuses.#"),
26+
resource.TestCheckResourceAttrSet(dataSource, "close_account_statuses.0.account_id"),
27+
resource.TestCheckResourceAttrSet(dataSource, "close_account_statuses.0.state"),
28+
resource.TestCheckResourceAttrSet(dataSource, "close_account_statuses.0.organization_id"),
29+
resource.TestCheckResourceAttrSet(dataSource, "close_account_statuses.0.created_at"),
30+
resource.TestCheckResourceAttrSet(dataSource, "close_account_statuses.0.updated_at"),
31+
resource.TestCheckOutput("states_filter_is_useful", "true"),
32+
),
33+
},
34+
},
35+
})
36+
}
37+
38+
func testDataSourceOrganizationsCloseAccountStatus_basic() string {
39+
return `
40+
data "huaweicloud_organizations_close_account_status" "test" {}
41+
42+
locals {
43+
state = "pending_closure"
44+
}
45+
data "huaweicloud_organizations_close_account_status" "states_filter" {
46+
states = ["pending_closure"]
47+
}
48+
output "states_filter_is_useful" {
49+
value = length(data.huaweicloud_organizations_close_account_status.states_filter.close_account_statuses) > 0 && alltrue(
50+
[for v in data.huaweicloud_organizations_close_account_status.states_filter.close_account_statuses[*].state : v == local.state]
51+
)
52+
}
53+
`
54+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
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

Comments
 (0)