Skip to content

Commit

Permalink
add enterprise_project_id to vpc, eip, bandwidth (#585)
Browse files Browse the repository at this point in the history
  • Loading branch information
Jason-Zhang9309 authored Oct 13, 2020
1 parent 84234af commit 9c06241
Show file tree
Hide file tree
Showing 18 changed files with 254 additions and 67 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ require (
github.com/hashicorp/errwrap v1.0.0
github.com/hashicorp/go-cleanhttp v0.5.1
github.com/hashicorp/terraform-plugin-sdk v1.13.0
github.com/huaweicloud/golangsdk v0.0.0-20200922033829-b87a6e09ecf6
github.com/huaweicloud/golangsdk v0.0.0-20201010022156-88a65d817494
github.com/jen20/awspolicyequivalence v0.0.0-20170831201602-3d48364a137a
github.com/mitchellh/go-homedir v1.1.0
github.com/smartystreets/goconvey v0.0.0-20190222223459-a17d461953aa // indirect
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -129,8 +129,8 @@ github.com/hashicorp/yamux v0.0.0-20180604194846-3520598351bb h1:b5rjCoWHc7eqmAS
github.com/hashicorp/yamux v0.0.0-20180604194846-3520598351bb/go.mod h1:+NfK9FKeTrX5uv1uIXGdwYDTeHna2qgaIlx54MXqjAM=
github.com/hashicorp/yamux v0.0.0-20181012175058-2f1d1f20f75d h1:kJCB4vdITiW1eC1vq2e6IsrXKrZit1bv/TDYFGMp4BQ=
github.com/hashicorp/yamux v0.0.0-20181012175058-2f1d1f20f75d/go.mod h1:+NfK9FKeTrX5uv1uIXGdwYDTeHna2qgaIlx54MXqjAM=
github.com/huaweicloud/golangsdk v0.0.0-20200922033829-b87a6e09ecf6 h1:GUotFSAbS/6LQHSPPpoqs4eNDQtzWaoTFOvO9rFAUTY=
github.com/huaweicloud/golangsdk v0.0.0-20200922033829-b87a6e09ecf6/go.mod h1:fcOI5u+0f62JtJd7zkCch/Z57BNC6bhqb32TKuiF4r0=
github.com/huaweicloud/golangsdk v0.0.0-20201010022156-88a65d817494 h1:3nOQxuXHUIYRS94unYux1/K+l5p1w/OhcqNVGJ3ejAU=
github.com/huaweicloud/golangsdk v0.0.0-20201010022156-88a65d817494/go.mod h1:fcOI5u+0f62JtJd7zkCch/Z57BNC6bhqb32TKuiF4r0=
github.com/jen20/awspolicyequivalence v0.0.0-20170831201602-3d48364a137a h1:FyS/ubzBR5xJlnJGRTwe7GUHpJOR4ukYK3y+LFNffuA=
github.com/jen20/awspolicyequivalence v0.0.0-20170831201602-3d48364a137a/go.mod h1:uoIMjNxUfXi48Ci40IXkPRbghZ1vbti6v9LCbNqRgHY=
github.com/jmespath/go-jmespath v0.0.0-20160202185014-0b12d6b521d8/go.mod h1:Nht3zPeWKUH0NzdCt2Blrr5ys8VGpn0CEB0cQHVjt7k=
Expand Down
47 changes: 24 additions & 23 deletions huaweicloud/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,29 +30,30 @@ const (
)

type Config struct {
AccessKey string
SecretKey string
CACertFile string
ClientCertFile string
ClientKeyFile string
DomainID string
DomainName string
IdentityEndpoint string
Insecure bool
Password string
Region string
TenantID string
TenantName string
Token string
Username string
UserID string
AgencyName string
AgencyDomainName string
DelegatedProject string
Cloud string
MaxRetries int
TerraformVersion string
RegionClient bool
AccessKey string
SecretKey string
CACertFile string
ClientCertFile string
ClientKeyFile string
DomainID string
DomainName string
IdentityEndpoint string
Insecure bool
Password string
Region string
TenantID string
TenantName string
Token string
Username string
UserID string
AgencyName string
AgencyDomainName string
DelegatedProject string
Cloud string
MaxRetries int
TerraformVersion string
RegionClient bool
EnterpriseProjectID string

HwClient *golangsdk.ProviderClient
s3sess *session.Session
Expand Down
11 changes: 11 additions & 0 deletions huaweicloud/data_source_huaweicloud_vpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ func DataSourceVirtualPrivateCloudVpcV1() *schema.Resource {
Type: schema.TypeString,
Optional: true,
},
"enterprise_project_id": {
Type: schema.TypeString,
Optional: true,
},
"status": {
Type: schema.TypeString,
Optional: true,
Expand Down Expand Up @@ -74,6 +78,12 @@ func dataSourceVirtualPrivateCloudV1Read(d *schema.ResourceData, meta interface{
CIDR: d.Get("cidr").(string),
}

epsID := GetEnterpriseProjectID(d, config)

if epsID != "" {
listOpts.EnterpriseProjectID = epsID
}

refinedVpcs, err := vpcs.List(vpcClient, listOpts)
if err != nil {
return fmt.Errorf("Unable to retrieve vpcs: %s", err)
Expand Down Expand Up @@ -105,6 +115,7 @@ func dataSourceVirtualPrivateCloudV1Read(d *schema.ResourceData, meta interface{

d.Set("name", Vpc.Name)
d.Set("cidr", Vpc.CIDR)
d.Set("enterprise_project_id", Vpc.EnterpriseProjectID)
d.Set("status", Vpc.Status)
d.Set("id", Vpc.ID)
d.Set("shared", Vpc.EnableSharedSnat)
Expand Down
53 changes: 31 additions & 22 deletions huaweicloud/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,12 @@ func Provider() terraform.ResourceProvider {
Default: 5,
Description: descriptions["max_retries"],
},
"enterprise_project_id": {
Type: schema.TypeString,
Optional: true,
DefaultFunc: schema.EnvDefaultFunc("OS_ENTERPRISE_PROJECT_ID", ""),
Description: descriptions["enterprise_project_id"],
},
},

DataSourcesMap: map[string]*schema.Resource{
Expand Down Expand Up @@ -538,6 +544,8 @@ func init() {
"cloud": "The endpoint of cloud provider, defaults to myhuaweicloud.com",

"max_retries": "How many times HTTP connection should be retried until giving up.",

"enterprise_project_id": "enterprise project id",
}
}

Expand All @@ -559,28 +567,29 @@ func configureProvider(d *schema.ResourceData, terraformVersion string) (interfa
}

config := Config{
AccessKey: d.Get("access_key").(string),
SecretKey: d.Get("secret_key").(string),
CACertFile: d.Get("cacert_file").(string),
ClientCertFile: d.Get("cert").(string),
ClientKeyFile: d.Get("key").(string),
DomainID: d.Get("domain_id").(string),
DomainName: d.Get("domain_name").(string),
IdentityEndpoint: d.Get("auth_url").(string),
Insecure: d.Get("insecure").(bool),
Password: d.Get("password").(string),
Region: d.Get("region").(string),
Token: d.Get("token").(string),
TenantID: d.Get("tenant_id").(string),
TenantName: tenant_name,
Username: d.Get("user_name").(string),
UserID: d.Get("user_id").(string),
AgencyName: d.Get("agency_name").(string),
AgencyDomainName: d.Get("agency_domain_name").(string),
DelegatedProject: delegated_project,
Cloud: d.Get("cloud").(string),
MaxRetries: d.Get("max_retries").(int),
TerraformVersion: terraformVersion,
AccessKey: d.Get("access_key").(string),
SecretKey: d.Get("secret_key").(string),
CACertFile: d.Get("cacert_file").(string),
ClientCertFile: d.Get("cert").(string),
ClientKeyFile: d.Get("key").(string),
DomainID: d.Get("domain_id").(string),
DomainName: d.Get("domain_name").(string),
IdentityEndpoint: d.Get("auth_url").(string),
Insecure: d.Get("insecure").(bool),
Password: d.Get("password").(string),
Region: d.Get("region").(string),
Token: d.Get("token").(string),
TenantID: d.Get("tenant_id").(string),
TenantName: tenant_name,
Username: d.Get("user_name").(string),
UserID: d.Get("user_id").(string),
AgencyName: d.Get("agency_name").(string),
AgencyDomainName: d.Get("agency_domain_name").(string),
DelegatedProject: delegated_project,
Cloud: d.Get("cloud").(string),
MaxRetries: d.Get("max_retries").(int),
EnterpriseProjectID: d.Get("enterprise_project_id").(string),
TerraformVersion: terraformVersion,
}

if err := config.LoadAndValidate(); err != nil {
Expand Down
7 changes: 7 additions & 0 deletions huaweicloud/provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ var (
OS_CCI_ENVIRONMENT = os.Getenv("OS_CCI_ENVIRONMENT")
OS_CDN_DOMAIN_NAME = os.Getenv("OS_CDN_DOMAIN_NAME")
OS_ADMIN = os.Getenv("OS_ADMIN")
OS_ENTERPRISE_PROJECT_ID = os.Getenv("OS_ENTERPRISE_PROJECT_ID")
)

var testAccProviders map[string]terraform.ResourceProvider
Expand Down Expand Up @@ -148,6 +149,12 @@ func testAccPreCheckCCI(t *testing.T) {
}
}

func testAccPreCheckEpsID(t *testing.T) {
if OS_ENTERPRISE_PROJECT_ID == "" {
t.Skip("This environment does not support EPS_ID tests")
}
}

func TestProvider(t *testing.T) {
if err := Provider().(*schema.Provider).InternalValidate(); err != nil {
t.Fatalf("err: %s", err)
Expand Down
13 changes: 13 additions & 0 deletions huaweicloud/resource_huaweicloud_vpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,12 @@ func ResourceVirtualPrivateCloudV1() *schema.Resource {
ForceNew: false,
ValidateFunc: validateCIDR,
},
"enterprise_project_id": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
Computed: true,
},
"status": {
Type: schema.TypeString,
Computed: true,
Expand Down Expand Up @@ -89,6 +95,12 @@ func resourceVirtualPrivateCloudV1Create(d *schema.ResourceData, meta interface{
CIDR: d.Get("cidr").(string),
}

epsID := GetEnterpriseProjectID(d, config)

if epsID != "" {
createOpts.EnterpriseProjectID = epsID
}

n, err := vpcs.Create(vpcClient, createOpts).Extract()

if err != nil {
Expand Down Expand Up @@ -149,6 +161,7 @@ func resourceVirtualPrivateCloudV1Read(d *schema.ResourceData, meta interface{})

d.Set("name", n.Name)
d.Set("cidr", n.CIDR)
d.Set("enterprise_project_id", n.EnterpriseProjectID)
d.Set("status", n.Status)
d.Set("shared", n.EnableSharedSnat)
d.Set("region", GetRegion(d, config))
Expand Down
12 changes: 9 additions & 3 deletions huaweicloud/resource_huaweicloud_vpc_bandwidth.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,16 @@ func resourceVpcBandWidthV2Create(d *schema.ResourceData, meta interface{}) erro
}

size := d.Get("size").(int)

createOpts := bandwidths.CreateOpts{
Name: d.Get("name").(string),
Size: &size,
EnterpriseProjectId: d.Get("enterprise_project_id").(string),
Name: d.Get("name").(string),
Size: &size,
}

epsID := GetEnterpriseProjectID(d, config)

if epsID != "" {
createOpts.EnterpriseProjectId = epsID
}

log.Printf("[DEBUG] Create Options: %#v", createOpts)
Expand Down
33 changes: 33 additions & 0 deletions huaweicloud/resource_huaweicloud_vpc_bandwidth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,29 @@ func TestAccVpcBandWidthV2_basic(t *testing.T) {
})
}

func TestAccVpcBandWidthV2_WithEpsId(t *testing.T) {
var bandwidth bandwidths.BandWidth

rName := fmt.Sprintf("tf-acc-test-%s", acctest.RandString(5))
resourceName := "huaweicloud_vpc_bandwidth.test"

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheckEpsID(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckVpcBandWidthV2Destroy,
Steps: []resource.TestStep{
{
Config: testAccVpcBandWidthV2_epsId(rName, 5),
Check: resource.ComposeTestCheckFunc(
testAccCheckVpcBandWidthV2Exists(resourceName, &bandwidth),
resource.TestCheckResourceAttr(resourceName, "name", rName),
resource.TestCheckResourceAttr(resourceName, "enterprise_project_id", OS_ENTERPRISE_PROJECT_ID),
),
},
},
})
}

func testAccCheckVpcBandWidthV2Destroy(s *terraform.State) error {
config := testAccProvider.Meta().(*Config)
networkingClient, err := config.NetworkingV1Client(OS_REGION_NAME)
Expand Down Expand Up @@ -104,3 +127,13 @@ resource "huaweicloud_vpc_bandwidth" "test" {
}
`, rName, size)
}

func testAccVpcBandWidthV2_epsId(rName string, size int) string {
return fmt.Sprintf(`
resource "huaweicloud_vpc_bandwidth" "test" {
name = "%s"
size = "%d"
enterprise_project_id = "%s"
}
`, rName, size, OS_ENTERPRISE_PROJECT_ID)
}
13 changes: 13 additions & 0 deletions huaweicloud/resource_huaweicloud_vpc_eip.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,12 @@ func ResourceVpcEIPV1() *schema.Resource {
},
},
},
"enterprise_project_id": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
Computed: true,
},
"address": {
Type: schema.TypeString,
Computed: true,
Expand Down Expand Up @@ -128,6 +134,12 @@ func resourceVpcEIPV1Create(d *schema.ResourceData, meta interface{}) error {
MapValueSpecs(d),
}

epsID := GetEnterpriseProjectID(d, config)

if epsID != "" {
createOpts.EnterpriseProjectID = epsID
}

log.Printf("[DEBUG] Create Options: %#v", createOpts)
eIP, err := eips.Apply(networkingClient, createOpts).Extract()
if err != nil {
Expand Down Expand Up @@ -193,6 +205,7 @@ func resourceVpcEIPV1Read(d *schema.ResourceData, meta interface{}) error {
d.Set("bandwidth", bW)
d.Set("address", eIP.PublicAddress)
d.Set("region", GetRegion(d, config))
d.Set("enterprise_project_id", eIP.EnterpriseProjectID)

return nil
}
Expand Down
39 changes: 39 additions & 0 deletions huaweicloud/resource_huaweicloud_vpc_eip_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,28 @@ func TestAccVpcV1EIP_share(t *testing.T) {
})
}

func TestAccVpcV1EIP_WithEpsId(t *testing.T) {
var eip eips.PublicIp

rName := fmt.Sprintf("tf-acc-test-%s", acctest.RandString(5))
resourceName := "huaweicloud_vpc_eip.test"

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheckEpsID(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckVpcV1EIPDestroy,
Steps: []resource.TestStep{
{
Config: testAccVpcV1EIP_epsId(rName),
Check: resource.ComposeTestCheckFunc(
testAccCheckVpcV1EIPExists(resourceName, &eip),
resource.TestCheckResourceAttr(resourceName, "enterprise_project_id", OS_ENTERPRISE_PROJECT_ID),
),
},
},
})
}

func testAccCheckVpcV1EIPDestroy(s *terraform.State) error {
config := testAccProvider.Meta().(*Config)
networkingClient, err := config.NetworkingV1Client(OS_REGION_NAME)
Expand Down Expand Up @@ -122,6 +144,23 @@ resource "huaweicloud_vpc_eip" "test" {
`, rName)
}

func testAccVpcV1EIP_epsId(rName string) string {
return fmt.Sprintf(`
resource "huaweicloud_vpc_eip" "test" {
publicip {
type = "5_bgp"
}
bandwidth {
name = "%s"
size = 8
share_type = "PER"
charge_mode = "traffic"
}
enterprise_project_id = "%s"
}
`, rName, OS_ENTERPRISE_PROJECT_ID)
}

func testAccVpcV1EIP_share(rName string) string {
return fmt.Sprintf(`
resource "huaweicloud_vpc_bandwidth" "test" {
Expand Down
Loading

0 comments on commit 9c06241

Please sign in to comment.