Skip to content

Commit

Permalink
add data source enterprise project support (#620)
Browse files Browse the repository at this point in the history
  • Loading branch information
Jason-Zhang9309 authored Nov 2, 2020
1 parent 5db4cc1 commit 8aadf2d
Show file tree
Hide file tree
Showing 10 changed files with 270 additions and 0 deletions.
51 changes: 51 additions & 0 deletions docs/data-sources/eps.md
Original file line number Diff line number Diff line change
@@ -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<br>huaweicloud_vpc_eip<br>huaweicloud_vpc_bandwidth<br>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<br>huaweicloud_gaussdb_mysql_instance<br>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

4 changes: 4 additions & 0 deletions huaweicloud/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
84 changes: 84 additions & 0 deletions huaweicloud/data_source_enterprise_project.go
Original file line number Diff line number Diff line change
@@ -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
}
49 changes: 49 additions & 0 deletions huaweicloud/data_source_enterprise_project_test.go
Original file line number Diff line number Diff line change
@@ -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"
}
`
7 changes: 7 additions & 0 deletions huaweicloud/endpoints.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down
1 change: 1 addition & 0 deletions huaweicloud/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions vendor/modules.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 8aadf2d

Please sign in to comment.