Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ECS instance v1 resource #179

Merged
merged 4 commits into from
Aug 12, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.0
github.com/hashicorp/terraform v0.12.0
github.com/huaweicloud/golangsdk v0.0.0-20190801094550-c3228f9b10d6
github.com/huaweicloud/golangsdk v0.0.0-20190809023913-080d32c8d1aa
github.com/jen20/awspolicyequivalence v0.0.0-20170831201602-3d48364a137a
github.com/mitchellh/go-homedir v1.0.0
github.com/smartystreets/assertions v0.0.0-20190116191733-b6c0e53d7304 // indirect
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -192,8 +192,8 @@ github.com/hashicorp/vault v0.10.4/go.mod h1:KfSyffbKxoVyspOdlaGVjIuwLobi07qD1bA
github.com/hashicorp/yamux v0.0.0-20180604194846-3520598351bb h1:b5rjCoWHc7eqmAS4/qyk21ZsHyb6Mxv/jykxvNTkU4M=
github.com/hashicorp/yamux v0.0.0-20180604194846-3520598351bb/go.mod h1:+NfK9FKeTrX5uv1uIXGdwYDTeHna2qgaIlx54MXqjAM=
github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU=
github.com/huaweicloud/golangsdk v0.0.0-20190801094550-c3228f9b10d6 h1:4ys1PB6+nv2Tm2bN8o0COXV8t4QU/GsM4X7VdWdnrHI=
github.com/huaweicloud/golangsdk v0.0.0-20190801094550-c3228f9b10d6/go.mod h1:WQBcHRNX9shz3928lWEvstQJtAtYI7ks6XlgtRT9Tcw=
github.com/huaweicloud/golangsdk v0.0.0-20190809023913-080d32c8d1aa h1:Wj+VsrMNgokKAoDJmzLqwx5TwctvU99nEvltoeEkG4Q=
github.com/huaweicloud/golangsdk v0.0.0-20190809023913-080d32c8d1aa/go.mod h1:WQBcHRNX9shz3928lWEvstQJtAtYI7ks6XlgtRT9Tcw=
github.com/jellevandenhooff/dkim v0.0.0-20150330215556-f50fe3d243e1/go.mod h1:E0B/fFc00Y+Rasa88328GlI/XbtyysCtTHZS8h7IrBU=
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=
Expand Down
14 changes: 14 additions & 0 deletions huaweicloud/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -421,6 +421,20 @@ func (c *Config) blockStorageV2Client(region string) (*golangsdk.ServiceClient,
})
}

func (c *Config) computeV1Client(region string) (*golangsdk.ServiceClient, error) {
return huaweisdk.NewComputeV1(c.HwClient, golangsdk.EndpointOpts{
Region: c.determineRegion(region),
Availability: c.getHwEndpointType(),
})
}

func (c *Config) computeV11Client(region string) (*golangsdk.ServiceClient, error) {
return huaweisdk.NewComputeV11(c.HwClient, golangsdk.EndpointOpts{
Region: c.determineRegion(region),
Availability: c.getHwEndpointType(),
})
}

func (c *Config) computeV2Client(region string) (*golangsdk.ServiceClient, error) {
return huaweisdk.NewComputeV2(c.HwClient, golangsdk.EndpointOpts{
Region: c.determineRegion(region),
Expand Down
34 changes: 34 additions & 0 deletions huaweicloud/ecs_tag_v1.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package huaweicloud

import (
"fmt"

"github.com/hashicorp/terraform/helper/schema"
"github.com/huaweicloud/golangsdk/openstack/ecs/v1/tags"
)

func setTagForInstance(d *schema.ResourceData, meta interface{}, instanceID string, tagmap map[string]interface{}) error {
config := meta.(*Config)
client, err := config.computeV1Client(GetRegion(d, config))
if err != nil {
return fmt.Errorf("Error creating HuaweiCloud compute v1 client: %s", err)
}

rId := instanceID
taglist := []tags.Tag{}
for k, v := range tagmap {
tag := tags.Tag{
Key: k,
Value: v.(string),
}
taglist = append(taglist, tag)
}

createOpts := tags.BatchOpts{Action: tags.ActionCreate, Tags: taglist}
createTags := tags.BatchAction(client, rId, createOpts)
if createTags.Err != nil {
return fmt.Errorf("Error creating HuaweiCloud instance tags: %s", createTags.Err)
}

return nil
}
1 change: 1 addition & 0 deletions huaweicloud/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,7 @@ func Provider() terraform.ResourceProvider {
"huaweicloud_dns_recordset_v2": resourceDNSRecordSetV2(),
"huaweicloud_dns_zone_v2": resourceDNSZoneV2(),
"huaweicloud_dcs_instance_v1": resourceDcsInstanceV1(),
"huaweicloud_ecs_instance_v1": resourceEcsInstanceV1(),
"huaweicloud_fw_firewall_group_v2": resourceFWFirewallGroupV2(),
"huaweicloud_fw_policy_v2": resourceFWPolicyV2(),
"huaweicloud_fw_rule_v2": resourceFWRuleV2(),
Expand Down
55 changes: 55 additions & 0 deletions huaweicloud/resource_huaweicloud_ecs_auto_recovery_v1.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package huaweicloud

import (
"fmt"
"log"
"strconv"

"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/helper/schema"
"github.com/huaweicloud/golangsdk/openstack/ecs/v1/auto_recovery"
)

func resourceECSAutoRecoveryV1Read(d *schema.ResourceData, meta interface{}, instanceID string) (bool, error) {
config := meta.(*Config)
client, err := config.computeV1Client(GetRegion(d, config))
if err != nil {
return false, fmt.Errorf("Error creating HuaweiCloud client: %s", err)
}

rId := instanceID

r, err := auto_recovery.Get(client, rId).Extract()
if err != nil {
return false, err
}
log.Printf("[DEBUG] Retrieved ECS-AutoRecovery:%#v of instance:%s", rId, r)
return strconv.ParseBool(r.SupportAutoRecovery)
}

func setAutoRecoveryForInstance(d *schema.ResourceData, meta interface{}, instanceID string, ar bool) error {
config := meta.(*Config)
client, err := config.computeV1Client(GetRegion(d, config))
if err != nil {
return fmt.Errorf("Error creating HuaweiCloud client: %s", err)
}

rId := instanceID

updateOpts := auto_recovery.UpdateOpts{SupportAutoRecovery: strconv.FormatBool(ar)}

timeout := d.Timeout(schema.TimeoutUpdate)

log.Printf("[DEBUG] Setting ECS-AutoRecovery for instance:%s with options: %#v", rId, updateOpts)
err = resource.Retry(timeout, func() *resource.RetryError {
err := auto_recovery.Update(client, rId, updateOpts)
if err != nil {
return checkForRetryableError(err)
}
return nil
})
if err != nil {
return fmt.Errorf("Error setting ECS-AutoRecovery for instance%s: %s", rId, err)
}
return nil
}
Loading