From 055c1abedd12c96aac8b1fb701b1ce44e85214f6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=88=98=E6=97=A5?= Date: Mon, 18 May 2020 17:43:51 +0800 Subject: [PATCH] A new resource for Resource Manager Resource Directory --- alicloud/provider.go | 1 + ...oud_resource_manager_resource_directory.go | 81 +++++++++++++++++++ ...esource_manager_resource_directory_test.go | 53 ++++++++++++ alicloud/service_alicloud_resourcemanager.go | 20 +++++ website/alicloud.erb | 3 + ...e_manager_resource_directory.html.markdown | 48 +++++++++++ 6 files changed, 206 insertions(+) create mode 100644 alicloud/resource_alicloud_resource_manager_resource_directory.go create mode 100644 alicloud/resource_alicloud_resource_manager_resource_directory_test.go create mode 100644 website/docs/r/resource_manager_resource_directory.html.markdown diff --git a/alicloud/provider.go b/alicloud/provider.go index cf36df8b1d73..d36c8b09eb45 100644 --- a/alicloud/provider.go +++ b/alicloud/provider.go @@ -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, diff --git a/alicloud/resource_alicloud_resource_manager_resource_directory.go b/alicloud/resource_alicloud_resource_manager_resource_directory.go new file mode 100644 index 000000000000..dbe857099181 --- /dev/null +++ b/alicloud/resource_alicloud_resource_manager_resource_directory.go @@ -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 +} diff --git a/alicloud/resource_alicloud_resource_manager_resource_directory_test.go b/alicloud/resource_alicloud_resource_manager_resource_directory_test.go new file mode 100644 index 000000000000..e6fa2a96e20b --- /dev/null +++ b/alicloud/resource_alicloud_resource_manager_resource_directory_test.go @@ -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 "" +} diff --git a/alicloud/service_alicloud_resourcemanager.go b/alicloud/service_alicloud_resourcemanager.go index 607d61314ea8..5cebb501bed7 100644 --- a/alicloud/service_alicloud_resourcemanager.go +++ b/alicloud/service_alicloud_resourcemanager.go @@ -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 +} diff --git a/website/alicloud.erb b/website/alicloud.erb index dc79db53dcd1..b7a10cab95e5 100644 --- a/website/alicloud.erb +++ b/website/alicloud.erb @@ -1503,6 +1503,9 @@
  • alicloud_resource_manager_policy
  • +
  • + alicloud_resource_manager_resource_directory +
  • alicloud_resource_manager_resource_group
  • diff --git a/website/docs/r/resource_manager_resource_directory.html.markdown b/website/docs/r/resource_manager_resource_directory.html.markdown new file mode 100644 index 000000000000..580b2a237652 --- /dev/null +++ b/website/docs/r/resource_manager_resource_directory.html.markdown @@ -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**** +```