Skip to content

feat(Organizations): add organizations close account status data source #6699

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions docs/data-sources/organizations_close_account_status.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
---
subcategory: "Organizations"
layout: "huaweicloud"
page_title: "HuaweiCloud: huaweicloud_organizations_close_account_status"
description: |-
Use this data source to get the list of the account close requests for an organization.
---

# huaweicloud_organizations_close_account_status

Use this data source to get the list of the account close requests for an organization.

## Example Usage

```hcl
data "huaweicloud_organizations_close_account_status" "test" {}
```

## Argument Reference

The following arguments are supported:

* `states` - (Optional, List) Specifies the list of one or more states that you want to include in the response.

## Attribute Reference

In addition to all arguments above, the following attributes are exported:

* `id` - The data source ID.

* `close_account_statuses` - Indicates the list of close account statuses.

The [close_account_statuses](#close_account_statuses_struct) structure is documented below.

<a name="close_account_statuses_struct"></a>
The `close_account_statuses` block supports:

* `account_id` - Indicates the ID of an account.

* `state` - Indicates the Status of the close account request.

* `organization_id` - Indicates the ID of an organization.

* `failure_reason` - Indicates the reason for a request failure.

* `created_at` - Indicates the date and time when the close account request was made.

* `updated_at` - Indicates the date and time when the status of close account request was updated.
1 change: 1 addition & 0 deletions huaweicloud/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -1451,6 +1451,7 @@ func Provider() *schema.Provider {
"huaweicloud_organizations_resource_instances": organizations.DataSourceOrganizationsResourceInstances(),
"huaweicloud_organizations_quotas": organizations.DataSourceOrganizationsQuotas(),
"huaweicloud_organizations_create_account_status": organizations.DataSourceOrganizationsCreateAccountStatus(),
"huaweicloud_organizations_close_account_status": organizations.DataSourceOrganizationsCloseAccountStatus(),

// Deprecated ongoing (without DeprecationMessage), used by other providers
"huaweicloud_vpc_route": vpc.DataSourceVpcRouteV2(),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package organizations

import (
"testing"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"

"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/services/acceptance"
)

func TestAccDataSourceOrganizationsCloseAccountStatus_basic(t *testing.T) {
dataSource := "data.huaweicloud_organizations_close_account_status.test"
dc := acceptance.InitDataSourceCheck(dataSource)

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() {
acceptance.TestAccPreCheck(t)
},
ProviderFactories: acceptance.TestAccProviderFactories,
Steps: []resource.TestStep{
{
Config: testDataSourceOrganizationsCloseAccountStatus_basic(),
Check: resource.ComposeTestCheckFunc(
dc.CheckResourceExists(),
resource.TestCheckResourceAttrSet(dataSource, "close_account_statuses.#"),
resource.TestCheckResourceAttrSet(dataSource, "close_account_statuses.0.account_id"),
resource.TestCheckResourceAttrSet(dataSource, "close_account_statuses.0.state"),
resource.TestCheckResourceAttrSet(dataSource, "close_account_statuses.0.organization_id"),
resource.TestCheckResourceAttrSet(dataSource, "close_account_statuses.0.created_at"),
resource.TestCheckResourceAttrSet(dataSource, "close_account_statuses.0.updated_at"),
resource.TestCheckOutput("states_filter_is_useful", "true"),
),
},
},
})
}

func testDataSourceOrganizationsCloseAccountStatus_basic() string {
return `
data "huaweicloud_organizations_close_account_status" "test" {}

locals {
state = "pending_closure"
}
data "huaweicloud_organizations_close_account_status" "states_filter" {
states = ["pending_closure"]
}
output "states_filter_is_useful" {
value = length(data.huaweicloud_organizations_close_account_status.states_filter.close_account_statuses) > 0 && alltrue(
[for v in data.huaweicloud_organizations_close_account_status.states_filter.close_account_statuses[*].state : v == local.state]
)
}
`
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
// Generated by PMS #612
package organizations

import (
"context"

"github.com/hashicorp/go-multierror"
"github.com/hashicorp/go-uuid"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/tidwall/gjson"

"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/config"
"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/helper/httphelper"
"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/helper/schemas"
"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/utils"
)

func DataSourceOrganizationsCloseAccountStatus() *schema.Resource {
return &schema.Resource{
ReadContext: dataSourceOrganizationsCloseAccountStatusRead,

Schema: map[string]*schema.Schema{
"states": {
Type: schema.TypeList,
Optional: true,
Elem: &schema.Schema{Type: schema.TypeString},
Description: `Specifies the list of one or more states that you want to include in the response.`,
},
"close_account_statuses": {
Type: schema.TypeList,
Computed: true,
Description: `Indicates the list of close account statuses.`,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"account_id": {
Type: schema.TypeString,
Computed: true,
Description: `Indicates the ID of an account.`,
},
"state": {
Type: schema.TypeString,
Computed: true,
Description: `Indicates the Status of the close account request.`,
},
"organization_id": {
Type: schema.TypeString,
Computed: true,
Description: `Indicates the ID of an organization.`,
},
"failure_reason": {
Type: schema.TypeString,
Computed: true,
Description: `Indicates the reason for a request failure.`,
},
"created_at": {
Type: schema.TypeString,
Computed: true,
Description: `Indicates the date and time when the close account request was made.`,
},
"updated_at": {
Type: schema.TypeString,
Computed: true,
Description: `Indicates the date and time when the status of close account request was updated.`,
},
},
},
},
},
}
}

type CloseAccountStatusDSWrapper struct {
*schemas.ResourceDataWrapper
Config *config.Config
}

func newCloseAccountStatusDSWrapper(d *schema.ResourceData, meta interface{}) *CloseAccountStatusDSWrapper {
return &CloseAccountStatusDSWrapper{
ResourceDataWrapper: schemas.NewSchemaWrapper(d),
Config: meta.(*config.Config),
}
}

func dataSourceOrganizationsCloseAccountStatusRead(_ context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
wrapper := newCloseAccountStatusDSWrapper(d, meta)
lisCloAccStaRst, err := wrapper.ListCloseAccountStatuses()
if err != nil {
return diag.FromErr(err)
}

id, err := uuid.GenerateUUID()
if err != nil {
return diag.FromErr(err)
}
d.SetId(id)

err = wrapper.listCloseAccountStatusesToSchema(lisCloAccStaRst)
if err != nil {
return diag.FromErr(err)
}

return nil
}

// @API Organizations GET /v1/organizations/close-account-status
func (w *CloseAccountStatusDSWrapper) ListCloseAccountStatuses() (*gjson.Result, error) {
client, err := w.NewClient(w.Config, "organizations")
if err != nil {
return nil, err
}

uri := "/v1/organizations/close-account-status"
params := map[string]any{
"states": w.ListToArray("states"),
}
params = utils.RemoveNil(params)
return httphelper.New(client).
Method("GET").
URI(uri).
Query(params).
Request().
Result()
}

func (w *CloseAccountStatusDSWrapper) listCloseAccountStatusesToSchema(body *gjson.Result) error {
d := w.ResourceData
mErr := multierror.Append(nil,
d.Set("close_account_statuses", schemas.SliceToList(body.Get("close_account_statuses"),
func(cloAccStatuses gjson.Result) any {
return map[string]any{
"account_id": cloAccStatuses.Get("account_id").Value(),
"state": cloAccStatuses.Get("state").Value(),
"organization_id": cloAccStatuses.Get("organization_id").Value(),
"failure_reason": cloAccStatuses.Get("failure_reason").Value(),
"created_at": cloAccStatuses.Get("created_at").Value(),
"updated_at": cloAccStatuses.Get("updated_at").Value(),
}
},
)),
)
return mErr.ErrorOrNil()
}
Loading