Skip to content

Commit

Permalink
Merge pull request #101 from yunify/update-string-to-const
Browse files Browse the repository at this point in the history
Update attribute string to const
  • Loading branch information
runzexia authored Nov 28, 2017
2 parents 6c8ec82 + 2d0d630 commit 185e505
Show file tree
Hide file tree
Showing 23 changed files with 452 additions and 456 deletions.
108 changes: 37 additions & 71 deletions qingcloud/resource_qingcloud_eip.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,14 @@ import (
"time"
)

const (
resourceEipBandwidth = "bandwidth"
resourceEipBillMode = "billing_mode"
resourceEipNeedIcp = "need_icp"
resourceEipAddr = "addr"
resourceEipResource = "resource"
)

func resourceQingcloudEip() *schema.Resource {
return &schema.Resource{
Create: resourceQingcloudEipCreate,
Expand All @@ -17,51 +25,39 @@ func resourceQingcloudEip() *schema.Resource {
Delete: resourceQingcloudEipDelete,
Schema: map[string]*schema.Schema{
resourceName: &schema.Schema{
Type: schema.TypeString,
Optional: true,
Description: "the name of eip",
Type: schema.TypeString,
Optional: true,
},
resourceDescription: &schema.Schema{
Type: schema.TypeString,
Optional: true,
Description: "the description of eip",
Type: schema.TypeString,
Optional: true,
},
"bandwidth": &schema.Schema{
Type: schema.TypeInt,
Required: true,
Description: "Maximum bandwidth to the elastic public network, measured in Mbps",
resourceEipBandwidth: &schema.Schema{
Type: schema.TypeInt,
Required: true,
},
"billing_mode": &schema.Schema{
resourceEipBillMode: &schema.Schema{
Type: schema.TypeString,
Optional: true,
Default: "bandwidth",
Description: "Internet charge type of the EIP : bandwidth , traffic ,default bandwidth",
ValidateFunc: withinArrayString("traffic", "bandwidth"),
},
"need_icp": &schema.Schema{
resourceEipNeedIcp: &schema.Schema{
Type: schema.TypeInt,
Optional: true,
Default: 0,
Description: "need icp , 1 need , 0 no need ,default 0",
ValidateFunc: withinArrayInt(0, 1),
},
resourceTagIds: tagIdsSchema(),
resourceTagNames: tagNamesSchema(),
"addr": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "ip address of this eip",
},
"status": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "the status of eip",
resourceEipAddr: &schema.Schema{
Type: schema.TypeString,
Computed: true,
},
"resource": &schema.Schema{
resourceEipResource: &schema.Schema{
Type: schema.TypeMap,
Computed: true,
ComputedWhen: []string{"id"},
Description: "the resource who use this eip",
},
},
}
Expand All @@ -70,9 +66,9 @@ func resourceQingcloudEip() *schema.Resource {
func resourceQingcloudEipCreate(d *schema.ResourceData, meta interface{}) error {
clt := meta.(*QingCloudClient).eip
input := new(qc.AllocateEIPsInput)
input.Bandwidth = qc.Int(d.Get("bandwidth").(int))
input.BillingMode = qc.String(d.Get("billing_mode").(string))
input.NeedICP = qc.Int(d.Get("need_icp").(int))
input.Bandwidth = qc.Int(d.Get(resourceEipBandwidth).(int))
input.BillingMode = qc.String(d.Get(resourceEipBillMode).(string))
input.NeedICP = qc.Int(d.Get(resourceEipNeedIcp).(int))
input.Count = qc.Int(1)
input.EIPName, _ = getNamePointer(d)
var output *qc.AllocateEIPsOutput
Expand Down Expand Up @@ -105,71 +101,41 @@ func resourceQingcloudEipRead(d *schema.ResourceData, meta interface{}) error {
if err != nil {
return err
}
if len(output.EIPSet) == 0 || qc.StringValue(output.EIPSet[0].Status) == "ceased" || qc.StringValue(output.EIPSet[0].Status) == "released" {
if isEipDeleted(output.EIPSet) {
d.SetId("")
return nil
}
ip := output.EIPSet[0]
d.Set(resourceName, qc.StringValue(ip.EIPName))
d.Set("billing_mode", qc.StringValue(ip.BillingMode))
d.Set("bandwidth", qc.IntValue(ip.Bandwidth))
d.Set("need_icp", qc.IntValue(ip.NeedICP))
d.Set(resourceEipBillMode, qc.StringValue(ip.BillingMode))
d.Set(resourceEipBandwidth, qc.IntValue(ip.Bandwidth))
d.Set(resourceEipNeedIcp, qc.IntValue(ip.NeedICP))
d.Set(resourceDescription, qc.StringValue(ip.Description))
// 如下状态是稍等来获取的
d.Set("addr", qc.StringValue(ip.EIPAddr))
d.Set("status", qc.StringValue(ip.Status))
if err := d.Set("resource", getEIPResourceMap(ip)); err != nil {
d.Set(resourceEipAddr, qc.StringValue(ip.EIPAddr))
if err := d.Set(resourceEipResource, getEIPResourceMap(ip)); err != nil {
return fmt.Errorf("Error set eip resource %v", err)
}
resourceSetTag(d, ip.Tags)
return nil
}

func resourceQingcloudEipUpdate(d *schema.ResourceData, meta interface{}) error {
clt := meta.(*QingCloudClient).eip
d.Partial(true)
if err := waitEipLease(d, meta); err != nil {
return err
}
if d.HasChange("need_icp") && !d.IsNewResource() {
if d.HasChange(resourceEipNeedIcp) && !d.IsNewResource() {
return fmt.Errorf("Errorf EIP need_icp could not be updated")
}
if d.HasChange("bandwidth") && !d.IsNewResource() {
input := new(qc.ChangeEIPsBandwidthInput)
input.EIPs = []*string{qc.String(d.Id())}
input.Bandwidth = qc.Int(d.Get("bandwidth").(int))
var output *qc.ChangeEIPsBandwidthOutput
var err error
simpleRetry(func() error {
output, err = clt.ChangeEIPsBandwidth(input)
return isServerBusy(err)
})
if err != nil {
return err
}
if _, err := EIPTransitionStateRefresh(clt, d.Id()); err != nil {
return nil
}
d.SetPartial("bandwidth")
if err := changeEIPBandwidth(d, meta); err != nil {
return err
}
if d.HasChange("billing_mode") && !d.IsNewResource() {
input := new(qc.ChangeEIPsBillingModeInput)
input.EIPs = []*string{qc.String(d.Id())}
input.BillingMode = qc.String(d.Get("billing_mode").(string))
var output *qc.ChangeEIPsBillingModeOutput
var err error
simpleRetry(func() error {
output, err = clt.ChangeEIPsBillingMode(input)
return isServerBusy(err)
})
if err != nil {
return err
}
if _, err := EIPTransitionStateRefresh(clt, d.Id()); err != nil {
return nil
}
d.SetPartial("billing_mode")
d.SetPartial(resourceEipBandwidth)
if err := changeEIPBillMode(d, meta); err != nil {
return err
}
d.SetPartial(resourceEipBillMode)
if err := modifyEipAttributes(d, meta); err != nil {
return err
}
Expand Down
49 changes: 49 additions & 0 deletions qingcloud/resource_qingcloud_eip_help.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,48 @@ func modifyEipAttributes(d *schema.ResourceData, meta interface{}) error {
}
return nil
}
func changeEIPBandwidth(d *schema.ResourceData, meta interface{}) error {
if d.HasChange(resourceEipBandwidth) && !d.IsNewResource() {
clt := meta.(*QingCloudClient).eip
input := new(qc.ChangeEIPsBandwidthInput)
input.EIPs = []*string{qc.String(d.Id())}
input.Bandwidth = qc.Int(d.Get(resourceEipBandwidth).(int))
var output *qc.ChangeEIPsBandwidthOutput
var err error
simpleRetry(func() error {
output, err = clt.ChangeEIPsBandwidth(input)
return isServerBusy(err)
})
if err != nil {
return err
}
if _, err := EIPTransitionStateRefresh(clt, d.Id()); err != nil {
return nil
}
}
return nil
}
func changeEIPBillMode(d *schema.ResourceData, meta interface{}) error {
clt := meta.(*QingCloudClient).eip
if d.HasChange(resourceEipBillMode) && !d.IsNewResource() {
input := new(qc.ChangeEIPsBillingModeInput)
input.EIPs = []*string{qc.String(d.Id())}
input.BillingMode = qc.String(d.Get(resourceEipBillMode).(string))
var output *qc.ChangeEIPsBillingModeOutput
var err error
simpleRetry(func() error {
output, err = clt.ChangeEIPsBillingMode(input)
return isServerBusy(err)
})
if err != nil {
return err
}
if _, err := EIPTransitionStateRefresh(clt, d.Id()); err != nil {
return nil
}
}
return nil
}

func getEIPResourceMap(data *qc.EIP) map[string]interface{} {
var a = make(map[string]interface{}, 3)
Expand All @@ -52,3 +94,10 @@ func waitEipLease(d *schema.ResourceData, meta interface{}) error {
WaitForLease(output.EIPSet[0].CreateTime)
return nil
}

func isEipDeleted(eipSet []*qc.EIP) bool {
if len(eipSet) == 0 || qc.StringValue(eipSet[0].Status) == "ceased" || qc.StringValue(eipSet[0].Status) == "ceased" {
return true
}
return false
}
Loading

0 comments on commit 185e505

Please sign in to comment.