From 1ee9df326ebd871d856ba38434bb19a9800e8a5c Mon Sep 17 00:00:00 2001 From: ShiChangkuo Date: Thu, 6 May 2021 15:50:29 +0800 Subject: [PATCH 1/3] deprecate cts resource and move to sub directory --- docs/data-sources/cts_tracker.md | 5 ++-- docs/resources/cts_tracker.md | 5 ++-- huaweicloud/config/config.go | 23 +++++++++++++++++++ huaweicloud/provider.go | 7 +++--- .../data_source_huaweicloud_cts_tracker_v1.go | 11 +++++---- .../resource_huaweicloud_cts_tracker_v1.go | 23 ++++++++++--------- 6 files changed, 50 insertions(+), 24 deletions(-) rename huaweicloud/{ => services/deprecated}/data_source_huaweicloud_cts_tracker_v1.go (91%) rename huaweicloud/{ => services/deprecated}/resource_huaweicloud_cts_tracker_v1.go (91%) diff --git a/docs/data-sources/cts_tracker.md b/docs/data-sources/cts_tracker.md index 3a6d0e35ef..b9c83b1f39 100644 --- a/docs/data-sources/cts_tracker.md +++ b/docs/data-sources/cts_tracker.md @@ -1,11 +1,12 @@ --- -subcategory: "Cloud Trace Service (CTS)" +subcategory: "Deprecated" --- # huaweicloud\_cts\_tracker +!> **Warning:** It has been deprecated. + CTS Tracker data source allows access of Cloud Tracker. -This is an alternative to `huaweicloud_cts_tracker_v1` ## Example Usage diff --git a/docs/resources/cts_tracker.md b/docs/resources/cts_tracker.md index 60f6085baa..aa3a1e49e8 100644 --- a/docs/resources/cts_tracker.md +++ b/docs/resources/cts_tracker.md @@ -1,11 +1,12 @@ --- -subcategory: "Cloud Trace Service (CTS)" +subcategory: "Deprecated" --- # huaweicloud\_cts\_tracker +!> **Warning:** It has been deprecated. + Allows you to collect, store, and query cloud resource operation records. -This is an alternative to `huaweicloud_cts_tracker_v1` ## Example Usage diff --git a/huaweicloud/config/config.go b/huaweicloud/config/config.go index e17d4487e9..cf21a4eb9a 100644 --- a/huaweicloud/config/config.go +++ b/huaweicloud/config/config.go @@ -10,6 +10,7 @@ import ( "github.com/hashicorp/terraform-plugin-sdk/helper/logging" "github.com/hashicorp/terraform-plugin-sdk/helper/pathorcontents" + "github.com/hashicorp/terraform-plugin-sdk/helper/schema" "github.com/huaweicloud/golangsdk" huaweisdk "github.com/huaweicloud/golangsdk/openstack" "github.com/huaweicloud/golangsdk/openstack/identity/v3/domains" @@ -513,6 +514,28 @@ func (c *Config) loadUserProjects(client *golangsdk.ProviderClient, region strin return nil } +// GetRegion returns the region that was specified in the resource. If a +// region was not set, the provider-level region is checked. The provider-level +// region can either be set by the region argument or by HW_REGION_NAME. +func (c *Config) GetRegion(d *schema.ResourceData) string { + if v, ok := d.GetOk("region"); ok { + return v.(string) + } + + return c.Region +} + +// GetEnterpriseProjectID returns the enterprise_project_id that was specified in the resource. +// If it was not set, the provider-level value is checked. The provider-level value can +// either be set by the `enterprise_project_id` argument or by HW_ENTERPRISE_PROJECT_ID. +func (c *Config) GetEnterpriseProjectID(d *schema.ResourceData) string { + if v, ok := d.GetOk("enterprise_project_id"); ok { + return v.(string) + } + + return c.EnterpriseProjectID +} + // ********** client for Global Service ********** func (c *Config) IAMV3Client(region string) (*golangsdk.ServiceClient, error) { return c.NewServiceClient("iam", region) diff --git a/huaweicloud/provider.go b/huaweicloud/provider.go index 44736eabf6..f5a5222ded 100644 --- a/huaweicloud/provider.go +++ b/huaweicloud/provider.go @@ -10,6 +10,7 @@ import ( "github.com/hashicorp/terraform-plugin-sdk/helper/schema" "github.com/hashicorp/terraform-plugin-sdk/terraform" "github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/config" + "github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/services/deprecated" ) const defaultCloud string = "myhuaweicloud.com" @@ -265,7 +266,6 @@ func Provider() terraform.ResourceProvider { "huaweicloud_compute_instance": DataSourceComputeInstance(), "huaweicloud_csbs_backup": dataSourceCSBSBackupV1(), "huaweicloud_csbs_backup_policy": dataSourceCSBSBackupPolicyV1(), - "huaweicloud_cts_tracker": dataSourceCTSTrackerV1(), "huaweicloud_dcs_az": dataSourceDcsAZV1(), "huaweicloud_dcs_maintainwindow": dataSourceDcsMaintainWindowV1(), "huaweicloud_dcs_product": dataSourceDcsProductV1(), @@ -331,7 +331,6 @@ func Provider() terraform.ResourceProvider { "huaweicloud_dms_maintainwindow_v1": dataSourceDmsMaintainWindowV1(), "huaweicloud_vbs_backup_policy_v2": dataSourceVBSBackupPolicyV2(), "huaweicloud_vbs_backup_v2": dataSourceVBSBackupV2(), - "huaweicloud_cts_tracker_v1": dataSourceCTSTrackerV1(), "huaweicloud_antiddos_v1": dataSourceAntiDdosV1(), "huaweicloud_dcs_az_v1": dataSourceDcsAZV1(), "huaweicloud_dcs_maintainwindow_v1": dataSourceDcsMaintainWindowV1(), @@ -347,6 +346,7 @@ func Provider() terraform.ResourceProvider { "huaweicloud_rts_stack_v1": dataSourceRTSStackV1(), "huaweicloud_rts_stack_resource_v1": dataSourceRTSStackResourcesV1(), "huaweicloud_rts_software_config_v1": dataSourceRtsSoftwareConfigV1(), + "huaweicloud_cts_tracker": deprecated.DataSourceCTSTrackerV1(), }, ResourcesMap: map[string]*schema.Resource{ @@ -381,7 +381,6 @@ func Provider() terraform.ResourceProvider { "huaweicloud_csbs_backup_policy": resourceCSBSBackupPolicyV1(), "huaweicloud_css_cluster": resourceCssClusterV1(), "huaweicloud_css_snapshot": resourceCssSnapshot(), - "huaweicloud_cts_tracker": resourceCTSTrackerV1(), "huaweicloud_dcs_instance": ResourceDcsInstanceV1(), "huaweicloud_dds_instance": resourceDdsInstanceV3(), "huaweicloud_dis_stream": resourceDisStreamV2(), @@ -536,7 +535,6 @@ func Provider() terraform.ResourceProvider { "huaweicloud_csbs_backup_policy_v1": resourceCSBSBackupPolicyV1(), "huaweicloud_vbs_backup_policy_v2": resourceVBSBackupPolicyV2(), "huaweicloud_vbs_backup_v2": resourceVBSBackupV2(), - "huaweicloud_cts_tracker_v1": resourceCTSTrackerV1(), "huaweicloud_maas_task": resourceMaasTaskV1(), "huaweicloud_maas_task_v1": resourceMaasTaskV1(), "huaweicloud_identity_project_v3": ResourceIdentityProjectV3(), @@ -577,6 +575,7 @@ func Provider() terraform.ResourceProvider { "huaweicloud_compute_floatingip_associate_v2": ResourceComputeFloatingIPAssociateV2(), "huaweicloud_rts_stack_v1": resourceRTSStackV1(), "huaweicloud_rts_software_config_v1": resourceSoftwareConfigV1(), + "huaweicloud_cts_tracker": deprecated.ResourceCTSTrackerV1(), }, } diff --git a/huaweicloud/data_source_huaweicloud_cts_tracker_v1.go b/huaweicloud/services/deprecated/data_source_huaweicloud_cts_tracker_v1.go similarity index 91% rename from huaweicloud/data_source_huaweicloud_cts_tracker_v1.go rename to huaweicloud/services/deprecated/data_source_huaweicloud_cts_tracker_v1.go index 61e6d76c89..b086c511e9 100644 --- a/huaweicloud/data_source_huaweicloud_cts_tracker_v1.go +++ b/huaweicloud/services/deprecated/data_source_huaweicloud_cts_tracker_v1.go @@ -1,4 +1,4 @@ -package huaweicloud +package deprecated import ( "fmt" @@ -9,9 +9,10 @@ import ( "github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/config" ) -func dataSourceCTSTrackerV1() *schema.Resource { +func DataSourceCTSTrackerV1() *schema.Resource { return &schema.Resource{ - Read: dataSourceCTSTrackerV1Read, + Read: dataSourceCTSTrackerV1Read, + DeprecationMessage: "CTS tracker has been deprecated.", Schema: map[string]*schema.Schema{ "region": { @@ -69,7 +70,7 @@ func dataSourceCTSTrackerV1() *schema.Resource { func dataSourceCTSTrackerV1Read(d *schema.ResourceData, meta interface{}) error { config := meta.(*config.Config) - trackerClient, err := config.CtsV1Client(GetRegion(d, config)) + trackerClient, err := config.CtsV1Client(config.GetRegion(d)) if err != nil { return fmt.Errorf("Error creating cts Client: %s", err) } @@ -113,7 +114,7 @@ func dataSourceCTSTrackerV1Read(d *schema.ResourceData, meta interface{}) error d.Set("operations", trackers.SimpleMessageNotification.Operations) d.Set("need_notify_user_list", trackers.SimpleMessageNotification.NeedNotifyUserList) - d.Set("region", GetRegion(d, config)) + d.Set("region", config.GetRegion(d)) return nil } diff --git a/huaweicloud/resource_huaweicloud_cts_tracker_v1.go b/huaweicloud/services/deprecated/resource_huaweicloud_cts_tracker_v1.go similarity index 91% rename from huaweicloud/resource_huaweicloud_cts_tracker_v1.go rename to huaweicloud/services/deprecated/resource_huaweicloud_cts_tracker_v1.go index c4e45badb6..b2e8974580 100644 --- a/huaweicloud/resource_huaweicloud_cts_tracker_v1.go +++ b/huaweicloud/services/deprecated/resource_huaweicloud_cts_tracker_v1.go @@ -1,4 +1,4 @@ -package huaweicloud +package deprecated import ( "time" @@ -13,12 +13,13 @@ import ( "github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/utils" ) -func resourceCTSTrackerV1() *schema.Resource { +func ResourceCTSTrackerV1() *schema.Resource { return &schema.Resource{ - Create: resourceCTSTrackerCreate, - Read: resourceCTSTrackerRead, - Update: resourceCTSTrackerUpdate, - Delete: resourceCTSTrackerDelete, + Create: resourceCTSTrackerCreate, + Read: resourceCTSTrackerRead, + Update: resourceCTSTrackerUpdate, + Delete: resourceCTSTrackerDelete, + DeprecationMessage: "CTS tracker has been deprecated.", Importer: &schema.ResourceImporter{ State: schema.ImportStatePassthrough, }, @@ -86,7 +87,7 @@ func resourceCTSTrackerV1() *schema.Resource { func resourceCTSTrackerCreate(d *schema.ResourceData, meta interface{}) error { config := meta.(*config.Config) - ctsClient, err := config.CtsV1Client(GetRegion(d, config)) + ctsClient, err := config.CtsV1Client(config.GetRegion(d)) if err != nil { return fmt.Errorf("Error creating cts Client: %s", err) @@ -121,7 +122,7 @@ func resourceCTSTrackerCreate(d *schema.ResourceData, meta interface{}) error { func resourceCTSTrackerRead(d *schema.ResourceData, meta interface{}) error { config := meta.(*config.Config) - ctsClient, err := config.CtsV1Client(GetRegion(d, config)) + ctsClient, err := config.CtsV1Client(config.GetRegion(d)) if err != nil { return fmt.Errorf("Error creating cts Client: %s", err) } @@ -154,14 +155,14 @@ func resourceCTSTrackerRead(d *schema.ResourceData, meta interface{}) error { d.Set("operations", ctsTracker.SimpleMessageNotification.Operations) d.Set("need_notify_user_list", ctsTracker.SimpleMessageNotification.NeedNotifyUserList) - d.Set("region", GetRegion(d, config)) + d.Set("region", config.GetRegion(d)) return nil } func resourceCTSTrackerUpdate(d *schema.ResourceData, meta interface{}) error { config := meta.(*config.Config) - ctsClient, err := config.CtsV1Client(GetRegion(d, config)) + ctsClient, err := config.CtsV1Client(config.GetRegion(d)) if err != nil { return fmt.Errorf("Error creating cts Client: %s", err) } @@ -199,7 +200,7 @@ func resourceCTSTrackerUpdate(d *schema.ResourceData, meta interface{}) error { func resourceCTSTrackerDelete(d *schema.ResourceData, meta interface{}) error { config := meta.(*config.Config) - ctsClient, err := config.CtsV1Client(GetRegion(d, config)) + ctsClient, err := config.CtsV1Client(config.GetRegion(d)) if err != nil { return fmt.Errorf("Error creating cts Client: %s", err) } From 0a230035c8d3a0f00757642a94f6dd1bb8f78e59 Mon Sep 17 00:00:00 2001 From: ShiChangkuo Date: Thu, 6 May 2021 17:30:35 +0800 Subject: [PATCH 2/3] update acceptance test for cts tracker --- huaweicloud/services/acceptance/acceptance.go | 63 +++++++++++++++++++ ..._source_huaweicloud_cts_tracker_v1_test.go | 7 ++- ...esource_huaweicloud_cts_tracker_v1_test.go | 15 ++--- 3 files changed, 75 insertions(+), 10 deletions(-) create mode 100644 huaweicloud/services/acceptance/acceptance.go rename huaweicloud/{ => services/acceptance/deprecated}/data_source_huaweicloud_cts_tracker_v1_test.go (89%) rename huaweicloud/{ => services/acceptance/deprecated}/resource_huaweicloud_cts_tracker_v1_test.go (88%) diff --git a/huaweicloud/services/acceptance/acceptance.go b/huaweicloud/services/acceptance/acceptance.go new file mode 100644 index 0000000000..5675c415e4 --- /dev/null +++ b/huaweicloud/services/acceptance/acceptance.go @@ -0,0 +1,63 @@ +package acceptance + +import ( + "os" + "testing" + + "github.com/hashicorp/terraform-plugin-sdk/helper/schema" + "github.com/hashicorp/terraform-plugin-sdk/terraform" + "github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud" +) + +var ( + HW_REGION_NAME = os.Getenv("HW_REGION_NAME") + HW_AVAILABILITY_ZONE = os.Getenv("HW_AVAILABILITY_ZONE") + HW_ACCESS_KEY = os.Getenv("HW_ACCESS_KEY") + HW_SECRET_KEY = os.Getenv("HW_SECRET_KEY") + HW_PROJECT_ID = os.Getenv("HW_PROJECT_ID") + HW_DOMAIN_ID = os.Getenv("HW_DOMAIN_ID") + HW_DOMAIN_NAME = os.Getenv("HW_DOMAIN_NAME") + + HW_FLAVOR_ID = os.Getenv("HW_FLAVOR_ID") + HW_FLAVOR_NAME = os.Getenv("HW_FLAVOR_NAME") + HW_IMAGE_ID = os.Getenv("HW_IMAGE_ID") + HW_IMAGE_NAME = os.Getenv("HW_IMAGE_NAME") + HW_VPC_ID = os.Getenv("HW_VPC_ID") + HW_NETWORK_ID = os.Getenv("HW_NETWORK_ID") + HW_SUBNET_ID = os.Getenv("HW_SUBNET_ID") + + HW_DEPRECATED_ENVIRONMENT = os.Getenv("HW_DEPRECATED_ENVIRONMENT") +) + +var TestAccProviders map[string]terraform.ResourceProvider +var TestAccProvider *schema.Provider + +func init() { + TestAccProvider = huaweicloud.Provider().(*schema.Provider) + TestAccProviders = map[string]terraform.ResourceProvider{ + "huaweicloud": TestAccProvider, + } +} + +func TestAccPreCheckRequiredEnvVars(t *testing.T) { + if HW_REGION_NAME == "" { + t.Fatal("HW_REGION_NAME must be set for acceptance tests") + } +} + +func TestAccPreCheck(t *testing.T) { + // Do not run the test if this is a deprecated testing environment. + if HW_DEPRECATED_ENVIRONMENT != "" { + t.Skip("This environment only runs deprecated tests") + } + + TestAccPreCheckRequiredEnvVars(t) +} + +func TestAccPreCheckDeprecated(t *testing.T) { + if HW_DEPRECATED_ENVIRONMENT == "" { + t.Skip("This environment does not support deprecated tests") + } + + TestAccPreCheckRequiredEnvVars(t) +} diff --git a/huaweicloud/data_source_huaweicloud_cts_tracker_v1_test.go b/huaweicloud/services/acceptance/deprecated/data_source_huaweicloud_cts_tracker_v1_test.go similarity index 89% rename from huaweicloud/data_source_huaweicloud_cts_tracker_v1_test.go rename to huaweicloud/services/acceptance/deprecated/data_source_huaweicloud_cts_tracker_v1_test.go index b3464734fd..18563d1f7c 100644 --- a/huaweicloud/data_source_huaweicloud_cts_tracker_v1_test.go +++ b/huaweicloud/services/acceptance/deprecated/data_source_huaweicloud_cts_tracker_v1_test.go @@ -1,4 +1,4 @@ -package huaweicloud +package deprecated import ( "fmt" @@ -6,14 +6,15 @@ import ( "github.com/hashicorp/terraform-plugin-sdk/helper/resource" "github.com/hashicorp/terraform-plugin-sdk/terraform" + "github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/services/acceptance" ) func TestAccCTSTrackerV1DataSource_basic(t *testing.T) { dataName := "data.huaweicloud_cts_tracker.tracker" resource.ParallelTest(t, resource.TestCase{ - PreCheck: func() { testAccPreCheck(t) }, - Providers: testAccProviders, + PreCheck: func() { acceptance.TestAccPreCheckDeprecated(t) }, + Providers: acceptance.TestAccProviders, Steps: []resource.TestStep{ { Config: testAccCTSTrackerV1DataSource_basic, diff --git a/huaweicloud/resource_huaweicloud_cts_tracker_v1_test.go b/huaweicloud/services/acceptance/deprecated/resource_huaweicloud_cts_tracker_v1_test.go similarity index 88% rename from huaweicloud/resource_huaweicloud_cts_tracker_v1_test.go rename to huaweicloud/services/acceptance/deprecated/resource_huaweicloud_cts_tracker_v1_test.go index 29c33498ef..b3593953a1 100644 --- a/huaweicloud/resource_huaweicloud_cts_tracker_v1_test.go +++ b/huaweicloud/services/acceptance/deprecated/resource_huaweicloud_cts_tracker_v1_test.go @@ -1,4 +1,4 @@ -package huaweicloud +package deprecated import ( "fmt" @@ -9,6 +9,7 @@ import ( "github.com/huaweicloud/golangsdk" "github.com/huaweicloud/golangsdk/openstack/cts/v1/tracker" "github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/config" + "github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/services/acceptance" ) func TestAccCTSTrackerV1_basic(t *testing.T) { @@ -16,8 +17,8 @@ func TestAccCTSTrackerV1_basic(t *testing.T) { resourceName := "huaweicloud_cts_tracker.tracker" resource.ParallelTest(t, resource.TestCase{ - PreCheck: func() { testAccPreCheck(t) }, - Providers: testAccProviders, + PreCheck: func() { acceptance.TestAccPreCheckDeprecated(t) }, + Providers: acceptance.TestAccProviders, CheckDestroy: testAccCheckCTSTrackerV1Destroy, Steps: []resource.TestStep{ { @@ -45,8 +46,8 @@ func TestAccCTSTrackerV1_basic(t *testing.T) { } func testAccCheckCTSTrackerV1Destroy(s *terraform.State) error { - config := testAccProvider.Meta().(*config.Config) - ctsClient, err := config.CtsV1Client(HW_REGION_NAME) + config := acceptance.TestAccProvider.Meta().(*config.Config) + ctsClient, err := config.CtsV1Client(config.GetRegion(nil)) if err != nil { return fmt.Errorf("Error creating cts client: %s", err) } @@ -79,8 +80,8 @@ func testAccCheckCTSTrackerV1Exists(n string, trackers *tracker.Tracker) resourc return fmt.Errorf("No ID is set") } - config := testAccProvider.Meta().(*config.Config) - ctsClient, err := config.CtsV1Client(HW_REGION_NAME) + config := acceptance.TestAccProvider.Meta().(*config.Config) + ctsClient, err := config.CtsV1Client(config.GetRegion(nil)) if err != nil { return fmt.Errorf("Error creating cts client: %s", err) } From b313862a1a1c85a021a8d386d3d6efc69b0051d0 Mon Sep 17 00:00:00 2001 From: ShiChangkuo Date: Thu, 6 May 2021 17:54:02 +0800 Subject: [PATCH 3/3] fix tfproviderlint issue --- huaweicloud/services/acceptance/acceptance.go | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/huaweicloud/services/acceptance/acceptance.go b/huaweicloud/services/acceptance/acceptance.go index 5675c415e4..d207228bfa 100644 --- a/huaweicloud/services/acceptance/acceptance.go +++ b/huaweicloud/services/acceptance/acceptance.go @@ -39,25 +39,27 @@ func init() { } } -func TestAccPreCheckRequiredEnvVars(t *testing.T) { +func preCheckRequiredEnvVars(t *testing.T) { if HW_REGION_NAME == "" { t.Fatal("HW_REGION_NAME must be set for acceptance tests") } } +//lintignore:AT003 func TestAccPreCheck(t *testing.T) { // Do not run the test if this is a deprecated testing environment. if HW_DEPRECATED_ENVIRONMENT != "" { t.Skip("This environment only runs deprecated tests") } - TestAccPreCheckRequiredEnvVars(t) + preCheckRequiredEnvVars(t) } +//lintignore:AT003 func TestAccPreCheckDeprecated(t *testing.T) { if HW_DEPRECATED_ENVIRONMENT == "" { t.Skip("This environment does not support deprecated tests") } - TestAccPreCheckRequiredEnvVars(t) + preCheckRequiredEnvVars(t) }