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 @@