Skip to content

Commit

Permalink
A new resource for Resource Manager Resource Directory
Browse files Browse the repository at this point in the history
  • Loading branch information
liuri1205 authored and xiaozhu36 committed May 18, 2020
1 parent a7d18a2 commit 055c1ab
Show file tree
Hide file tree
Showing 6 changed files with 206 additions and 0 deletions.
1 change: 1 addition & 0 deletions alicloud/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -499,6 +499,7 @@ func Provider() terraform.ResourceProvider {
"alicloud_resource_manager_policy": resourceAlicloudResourceManagerPolicy(),
"alicloud_resource_manager_account": resourceAlicloudResourceManagerAccount(),
"alicloud_waf_instance": resourceAlicloudWafInstance(),
"alicloud_resource_manager_resource_directory": resourceAlicloudResourceManagerResourceDirectory(),
},

ConfigureFunc: providerConfigure,
Expand Down
81 changes: 81 additions & 0 deletions alicloud/resource_alicloud_resource_manager_resource_directory.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package alicloud

import (
"fmt"

"github.com/aliyun/alibaba-cloud-sdk-go/services/resourcemanager"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/terraform-providers/terraform-provider-alicloud/alicloud/connectivity"
)

func resourceAlicloudResourceManagerResourceDirectory() *schema.Resource {
return &schema.Resource{
Create: resourceAlicloudResourceManagerResourceDirectoryCreate,
Read: resourceAlicloudResourceManagerResourceDirectoryRead,
Delete: resourceAlicloudResourceManagerResourceDirectoryDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
Schema: map[string]*schema.Schema{
"master_account_id": {
Type: schema.TypeString,
Computed: true,
},
"master_account_name": {
Type: schema.TypeString,
Computed: true,
},
"root_folder_id": {
Type: schema.TypeString,
Computed: true,
},
},
}
}

func resourceAlicloudResourceManagerResourceDirectoryCreate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*connectivity.AliyunClient)

request := resourcemanager.CreateInitResourceDirectoryRequest()
raw, err := client.WithResourcemanagerClient(func(resourcemanagerClient *resourcemanager.Client) (interface{}, error) {
return resourcemanagerClient.InitResourceDirectory(request)
})
if err != nil {
return WrapErrorf(err, DefaultErrorMsg, "alicloud_resource_manager_resource_directory", request.GetActionName(), AlibabaCloudSdkGoERROR)
}
addDebug(request.GetActionName(), raw)
response, _ := raw.(*resourcemanager.InitResourceDirectoryResponse)
d.SetId(fmt.Sprintf("%v", response.ResourceDirectory.ResourceDirectoryId))

return resourceAlicloudResourceManagerResourceDirectoryRead(d, meta)
}
func resourceAlicloudResourceManagerResourceDirectoryRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*connectivity.AliyunClient)
resourcemanagerService := ResourcemanagerService{client}
object, err := resourcemanagerService.DescribeResourceManagerResourceDirectory(d.Id())
if err != nil {
if NotFoundError(err) {
d.SetId("")
return nil
}
return WrapError(err)
}

d.Set("master_account_id", object.MasterAccountId)
d.Set("master_account_name", object.MasterAccountName)
d.Set("root_folder_id", object.RootFolderId)
return nil
}
func resourceAlicloudResourceManagerResourceDirectoryDelete(d *schema.ResourceData, meta interface{}) error {
client := meta.(*connectivity.AliyunClient)
request := resourcemanager.CreateDestroyResourceDirectoryRequest()

raw, err := client.WithResourcemanagerClient(func(resourcemanagerClient *resourcemanager.Client) (interface{}, error) {
return resourcemanagerClient.DestroyResourceDirectory(request)
})
addDebug(request.GetActionName(), raw)
if err != nil {
return WrapErrorf(err, DefaultErrorMsg, d.Id(), request.GetActionName(), AlibabaCloudSdkGoERROR)
}
return nil
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package alicloud

import (
"testing"

"github.com/aliyun/alibaba-cloud-sdk-go/services/resourcemanager"
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
"github.com/terraform-providers/terraform-provider-alicloud/alicloud/connectivity"
)

func TestAccAlicloudResourceManagerResourceDirectory_basic(t *testing.T) {
var v resourcemanager.ResourceDirectory
resourceId := "alicloud_resource_manager_resource_directory.default"
ra := resourceAttrInit(resourceId, ResourceManagerResourceDirectoryMap)
rc := resourceCheckInitWithDescribeMethod(resourceId, &v, func() interface{} {
return &ResourcemanagerService{testAccProvider.Meta().(*connectivity.AliyunClient)}
}, "DescribeResourceManagerResourceDirectory")
rac := resourceAttrCheckInit(rc, ra)
testAccCheck := rac.resourceAttrMapUpdateSet()
testAccConfig := resourceTestAccConfigFunc(resourceId, "", ResourceManagerResourceDirectoryBasicdependence)
resource.Test(t, resource.TestCase{
PreCheck: func() {
testAccPreCheck(t)
},

IDRefreshName: resourceId,
Providers: testAccProviders,
CheckDestroy: rac.checkResourceDestroy(),
Steps: []resource.TestStep{
{
Config: testAccConfig(map[string]interface{}{}),
Check: resource.ComposeTestCheckFunc(
testAccCheck(map[string]string{}),
),
},
{
ResourceName: resourceId,
ImportState: true,
ImportStateVerify: true,
},
},
})
}

var ResourceManagerResourceDirectoryMap = map[string]string{
"master_account_id": CHECKSET,
"master_account_name": CHECKSET,
"root_folder_id": CHECKSET,
}

func ResourceManagerResourceDirectoryBasicdependence(name string) string {
return ""
}
20 changes: 20 additions & 0 deletions alicloud/service_alicloud_resourcemanager.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,3 +195,23 @@ func (s *ResourcemanagerService) DescribeResourceManagerAccount(id string) (obje
response, _ := raw.(*resourcemanager.GetAccountResponse)
return response.Account, nil
}

func (s *ResourcemanagerService) DescribeResourceManagerResourceDirectory(id string) (object resourcemanager.ResourceDirectory, err error) {
request := resourcemanager.CreateGetResourceDirectoryRequest()
request.RegionId = s.client.RegionId

raw, err := s.client.WithResourcemanagerClient(func(resourcemanagerClient *resourcemanager.Client) (interface{}, error) {
return resourcemanagerClient.GetResourceDirectory(request)
})
if err != nil {
if IsExpectedErrors(err, []string{"ResourceDirectoryNotInUse"}) {
err = WrapErrorf(Error(GetNotFoundMessage("ResourceManagerResourceDirectory", id)), NotFoundMsg, ProviderERROR)
return
}
err = WrapErrorf(err, DefaultErrorMsg, id, request.GetActionName(), AlibabaCloudSdkGoERROR)
return
}
addDebug(request.GetActionName(), raw, request.RpcRequest, request)
response, _ := raw.(*resourcemanager.GetResourceDirectoryResponse)
return response.ResourceDirectory, nil
}
3 changes: 3 additions & 0 deletions website/alicloud.erb
Original file line number Diff line number Diff line change
Expand Up @@ -1503,6 +1503,9 @@
<li>
<a href="/docs/providers/alicloud/r/resource_manager_policy.html">alicloud_resource_manager_policy</a>
</li>
<li>
<a href="/docs/providers/alicloud/r/resource_manager_resource_directory.html">alicloud_resource_manager_resource_directory</a>
</li>
<li>
<a href="/docs/providers/alicloud/r/resource_manager_resource_group.html">alicloud_resource_manager_resource_group</a>
</li>
Expand Down
48 changes: 48 additions & 0 deletions website/docs/r/resource_manager_resource_directory.html.markdown
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
---
subcategory: "Resource Manager"
layout: "alicloud"
page_title: "Alicloud: alicloud_resource_manager_resource_directory"
sidebar_current: "docs-alicloud-resource-resource-manager-resource-directory"
description: |-
Provides a Alicloud Resource Manager Resource Directory resource.
---

# alicloud\_resource\_manager\_resource\_directory

Provides a Resource Manager Resource Directory resource. Resource Directory enables you to establish an organizational structure for the resources used by applications of your enterprise. You can plan, build, and manage the resources in a centralized manner by using only one resource directory.

For information about Resource Manager Resource Directory and how to use it, see [What is Resource Manager Resource Directory](https://www.alibabacloud.com/help/en/doc-detail/94475.htm).

-> **NOTE:** Available in v1.84.0+.

-> **NOTE:** An account can only be used to enable a resource directory after it passes enterprise real-name verification. An account that only passed individual real-name verification cannot be used to enable a resource directory.

-> **NOTE:** Before you destroy the resource, make sure that the following requirements are met:
- All member accounts must be removed from the resource directory.
- All folders except the root folder must be deleted from the resource directory.

## Example Usage

Basic Usage

```
resource "alicloud_resource_manager_resource_directory" "example" {}
```
## Argument Reference

The resource does not support any argument.

## Attributes Reference

* `id` - The ID of the resource directory.
* `master_account_id` - The ID of the master account.
* `master_account_name` - The name of the master account.
* `root_folder_id` - The ID of the root folder.

## Import

Resource Manager Resource Directory can be imported using the id, e.g.

```
$ terraform import alicloud_resource_manager_resource_directory.example rd-s3****
```

0 comments on commit 055c1ab

Please sign in to comment.