diff --git a/docs/data-sources/eps.md b/docs/data-sources/eps.md new file mode 100644 index 0000000000..759e020e31 --- /dev/null +++ b/docs/data-sources/eps.md @@ -0,0 +1,51 @@ +--- +subcategory: "Enterprise Project Management Service (EPS)" +--- + +# huaweicloud\_enterprise\_project + +Use this data source to get an enterprise project from HuaweiCloud + +## Example Usage + +```hcl +data "huaweicloud_enterprise_project" "test" { + name = "test" +} +``` + +## Resources Supported Currently: +Service Name | Resource Name +---- | --- +VPC | huaweicloud_vpc
huaweicloud_vpc_eip
huaweicloud_vpc_bandwidth
huaweicloud_networking_secgroup +ECS | huaweicloud_compute_instance +CCE | huaweicloud_cce_cluster +RDS | huaweicloud_rds_instance +OBS | hauweicloud_obs_bucket +SFS | hauweicloud_sfs_file_system +DCS | huaweicloud_dcs_instance +NAT | huaweicloud_nat_gateway +CDM | huaweicloud_cdm_cluster +CDN | huaweicloud_cdn_domain +GaussDB | huaweicloud_gaussdb_cassandra_instance
huaweicloud_gaussdb_mysql_instance
huaweicloud_gaussdb_opengauss_instance + +## Argument Reference + +* `name` - (Optional) Specifies the enterprise project name. Fuzzy search is supported. + +* `id` - (Optional) Specifies the ID of an enterprise project. The value 0 indicates enterprise project default. + +* `status` - (Optional) Specifies the status of an enterprise project. + - 1 indicates Enabled. + - 2 indicates Disabled. + +## Attributes Reference + +All above argument parameters can be exported as attribute parameters along with attribute reference: + +* `description` - Provides supplementary information about the enterprise project. + +* `created_at` - Specifies the time (UTC) when the enterprise project was created. Example: 2018-05-18T06:49:06Z + +* `updated_at` - Specifies the time (UTC) when the enterprise project was modified. Example: 2018-05-28T02:21:36Z + diff --git a/huaweicloud/config.go b/huaweicloud/config.go index 9824854e14..18cfe44c1d 100644 --- a/huaweicloud/config.go +++ b/huaweicloud/config.go @@ -461,6 +461,10 @@ func (c *Config) CdnV1Client(region string) (*golangsdk.ServiceClient, error) { return c.NewServiceClient("cdn", region) } +func (c *Config) EnterpriseProjectClient(region string) (*golangsdk.ServiceClient, error) { + return c.NewServiceClient("eps", region) +} + // ********** client for Compute ********** func (c *Config) computeV1Client(region string) (*golangsdk.ServiceClient, error) { return c.NewServiceClient("ecs", region) diff --git a/huaweicloud/data_source_enterprise_project.go b/huaweicloud/data_source_enterprise_project.go new file mode 100644 index 0000000000..ead7469cbe --- /dev/null +++ b/huaweicloud/data_source_enterprise_project.go @@ -0,0 +1,84 @@ +package huaweicloud + +import ( + "fmt" + + "github.com/hashicorp/terraform-plugin-sdk/helper/schema" + "github.com/huaweicloud/golangsdk/openstack/eps/v1/enterpriseprojects" +) + +func DataSourceEnterpriseProject() *schema.Resource { + return &schema.Resource{ + Read: dataSourceEnterpriseProjectRead, + Schema: map[string]*schema.Schema{ + "name": { + Type: schema.TypeString, + Optional: true, + Computed: true, + }, + "id": { + Type: schema.TypeString, + Optional: true, + Computed: true, + }, + "status": { + Type: schema.TypeInt, + Optional: true, + Computed: true, + }, + "description": { + Type: schema.TypeString, + Computed: true, + }, + "created_at": { + Type: schema.TypeString, + Computed: true, + }, + "updated_at": { + Type: schema.TypeString, + Computed: true, + }, + }, + } +} + +func dataSourceEnterpriseProjectRead(d *schema.ResourceData, meta interface{}) error { + config := meta.(*Config) + region := GetRegion(d, config) + epsClient, err := config.EnterpriseProjectClient(region) + if err != nil { + return fmt.Errorf("Error creating Huaweicloud eps client %s", err) + } + + listOpts := enterpriseprojects.ListOpts{ + Name: d.Get("name").(string), + ID: d.Get("id").(string), + Status: d.Get("status").(int), + } + projects, err := enterpriseprojects.List(epsClient, listOpts).Extract() + + if err != nil { + return fmt.Errorf("Error retriving enterprise projects %s", err) + } + + if len(projects) < 1 { + return fmt.Errorf("Your query returned no results. " + + "Please change your search criteria and try again.") + } + + if len(projects) > 1 { + return fmt.Errorf("Your query returned more than one result." + + " Please try a more specific search criteria") + } + + project := projects[0] + + d.SetId(project.ID) + d.Set("name", project.Name) + d.Set("description", project.Description) + d.Set("status", project.Status) + d.Set("created_at", project.CreatedAt) + d.Set("updated_at", project.UpdatedAt) + + return nil +} diff --git a/huaweicloud/data_source_enterprise_project_test.go b/huaweicloud/data_source_enterprise_project_test.go new file mode 100644 index 0000000000..251290f414 --- /dev/null +++ b/huaweicloud/data_source_enterprise_project_test.go @@ -0,0 +1,49 @@ +package huaweicloud + +import ( + "fmt" + "testing" + + "github.com/hashicorp/terraform-plugin-sdk/helper/resource" + "github.com/hashicorp/terraform-plugin-sdk/terraform" +) + +func TestAccEnterpriseProjectDataSource_basic(t *testing.T) { + resourceName := "data.huaweicloud_enterprise_project.test" + + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + Steps: []resource.TestStep{ + { + Config: testAccEnterpriseProjectDataSource_basic, + Check: resource.ComposeTestCheckFunc( + testAccCheckEnterpriseProjectDataSourceID(resourceName), + resource.TestCheckResourceAttr(resourceName, "name", "default"), + resource.TestCheckResourceAttr(resourceName, "id", "0"), + ), + }, + }, + }) +} + +func testAccCheckEnterpriseProjectDataSourceID(n string) resource.TestCheckFunc { + return func(s *terraform.State) error { + rs, ok := s.RootModule().Resources[n] + if !ok { + return fmt.Errorf("Can't find enterprise project data source: %s ", n) + } + + if rs.Primary.ID == "" { + return fmt.Errorf("enterprise project data source ID not set ") + } + + return nil + } +} + +const testAccEnterpriseProjectDataSource_basic = ` +data "huaweicloud_enterprise_project" "test" { + name = "default" +} +` diff --git a/huaweicloud/endpoints.go b/huaweicloud/endpoints.go index 0b3c72f721..17a8aa6035 100644 --- a/huaweicloud/endpoints.go +++ b/huaweicloud/endpoints.go @@ -42,6 +42,13 @@ var allServiceCatalog = map[string]ServiceCatalog{ Scope: "global", WithOutProjectID: true, }, + "eps": ServiceCatalog{ + Name: "eps", + Version: "v1.0", + Scope: "global", + Admin: true, + WithOutProjectID: true, + }, // ******* catalog for Compute ******* "ecs": ServiceCatalog{ diff --git a/huaweicloud/provider.go b/huaweicloud/provider.go index 888966cfca..6809f4aaef 100644 --- a/huaweicloud/provider.go +++ b/huaweicloud/provider.go @@ -207,6 +207,7 @@ func Provider() terraform.ResourceProvider { "huaweicloud_dms_az": dataSourceDmsAZV1(), "huaweicloud_dms_product": dataSourceDmsProductV1(), "huaweicloud_dms_maintainwindow": dataSourceDmsMaintainWindowV1(), + "huaweicloud_enterprise_project": DataSourceEnterpriseProject(), "huaweicloud_gaussdb_mysql_configuration": dataSourceGaussdbMysqlConfigurations(), "huaweicloud_gaussdb_mysql_flavors": dataSourceGaussdbMysqlFlavors(), "huaweicloud_iam_role": dataSourceIAMRoleV3(), diff --git a/vendor/github.com/huaweicloud/golangsdk/openstack/eps/v1/enterpriseprojects/requests.go b/vendor/github.com/huaweicloud/golangsdk/openstack/eps/v1/enterpriseprojects/requests.go new file mode 100644 index 0000000000..0ccd177f5b --- /dev/null +++ b/vendor/github.com/huaweicloud/golangsdk/openstack/eps/v1/enterpriseprojects/requests.go @@ -0,0 +1,34 @@ +package enterpriseprojects + +import ( + "github.com/huaweicloud/golangsdk" +) + +type ListOpts struct { + Name string `q:"name"` + ID string `q:"id"` + Status int `q:"status"` +} + +func (opts ListOpts) ToEnterpriseProjectListQuery() (string, error) { + q, err := golangsdk.BuildQueryString(opts) + return q.String(), err +} + +type ListOptsBuilder interface { + ToEnterpriseProjectListQuery() (string, error) +} + +func List(c *golangsdk.ServiceClient, opts ListOptsBuilder) (r ListResult) { + url := rootURL(c) + if opts != nil { + query, err := opts.ToEnterpriseProjectListQuery() + if err != nil { + r.Err = err + } + url += query + } + + _, r.Err = c.Get(url, &r.Body, nil) + return +} diff --git a/vendor/github.com/huaweicloud/golangsdk/openstack/eps/v1/enterpriseprojects/results.go b/vendor/github.com/huaweicloud/golangsdk/openstack/eps/v1/enterpriseprojects/results.go new file mode 100644 index 0000000000..ff05a0be74 --- /dev/null +++ b/vendor/github.com/huaweicloud/golangsdk/openstack/eps/v1/enterpriseprojects/results.go @@ -0,0 +1,30 @@ +package enterpriseprojects + +import "github.com/huaweicloud/golangsdk" + +type Project struct { + ID string `json:"id"` + Name string `json:"name"` + Description string `json:"description"` + Status int `json:"status"` + CreatedAt string `json:"created_at"` + UpdatedAt string `json:"updated_at"` + Type string `json:"type"` +} + +type Projects struct { + EnterpriseProjects []Project `json:"enterprise_projects"` + TotalCount int `json:"total_count"` +} + +type ListResult struct { + golangsdk.Result +} + +func (r ListResult) Extract() ([]Project, error) { + var a struct { + EnterpriseProjects []Project `json:"enterprise_projects"` + } + err := r.Result.ExtractInto(&a) + return a.EnterpriseProjects, err +} diff --git a/vendor/github.com/huaweicloud/golangsdk/openstack/eps/v1/enterpriseprojects/urls.go b/vendor/github.com/huaweicloud/golangsdk/openstack/eps/v1/enterpriseprojects/urls.go new file mode 100644 index 0000000000..513a6f2aff --- /dev/null +++ b/vendor/github.com/huaweicloud/golangsdk/openstack/eps/v1/enterpriseprojects/urls.go @@ -0,0 +1,9 @@ +package enterpriseprojects + +import "github.com/huaweicloud/golangsdk" + +const resourcePath = "enterprise-projects" + +func rootURL(c *golangsdk.ServiceClient) string { + return c.ServiceURL(resourcePath) +} diff --git a/vendor/modules.txt b/vendor/modules.txt index ed64324167..a0a9240418 100644 --- a/vendor/modules.txt +++ b/vendor/modules.txt @@ -248,6 +248,7 @@ github.com/huaweicloud/golangsdk/openstack/ecs/v1/auto_recovery github.com/huaweicloud/golangsdk/openstack/ecs/v1/block_devices github.com/huaweicloud/golangsdk/openstack/ecs/v1/cloudservers github.com/huaweicloud/golangsdk/openstack/ecs/v1/flavors +github.com/huaweicloud/golangsdk/openstack/eps/v1/enterpriseprojects github.com/huaweicloud/golangsdk/openstack/evs/v2/snapshots github.com/huaweicloud/golangsdk/openstack/evs/v2/tags github.com/huaweicloud/golangsdk/openstack/evs/v3/volumes