Skip to content

Commit

Permalink
Merge pull request #9 from fpt-corp/feature/improve_schema_type
Browse files Browse the repository at this point in the history
feat: logic for type set
  • Loading branch information
ducdm49 authored Aug 29, 2024
2 parents 705c77f + 9c0aba8 commit d93d62c
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 20 deletions.
7 changes: 6 additions & 1 deletion fptcloud/instance-group/resource_instance_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,12 @@ func resourceInstanceGroupCreate(ctx context.Context, d *schema.ResourceData, m
createModel.PolicyId = policyId.(string)
}
if vmIds, ok := d.GetOk("vm_ids"); ok {
createModel.VmIds = vmIds.([]string)
vmIdsSet := vmIds.(*schema.Set)
vmIdsList := make([]string, 0, len(vmIdsSet.List()))
for _, v := range vmIdsSet.List() {
vmIdsList = append(vmIdsList, v.(string))
}
createModel.VmIds = vmIdsList
}

isSuccess, err := service.CreateInstanceGroup(createModel)
Expand Down
9 changes: 5 additions & 4 deletions fptcloud/instance/resource_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,12 @@ func resourceInstanceCreate(ctx context.Context, d *schema.ResourceData, m inter
}

if securityGroupIds, ok := d.GetOk("security_group_ids"); ok {
securityGroupIdsValue := securityGroupIds.([]interface{})
createdModel.SecurityGroupIds = make([]string, len(securityGroupIdsValue))
for i, v := range securityGroupIdsValue {
createdModel.SecurityGroupIds[i] = v.(string)
securityGroupIdsSet := securityGroupIds.(*schema.Set)
securityGroupIdsList := make([]string, 0, len(securityGroupIdsSet.List()))
for _, v := range securityGroupIdsSet.List() {
securityGroupIdsList = append(securityGroupIdsList, v.(string))
}
createdModel.SecurityGroupIds = securityGroupIdsList
}

if instanceGroupId, ok := d.GetOk("instance_group_id"); ok {
Expand Down
2 changes: 1 addition & 1 deletion fptcloud/instance/schema_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ var dataSourceInstanceSchema = map[string]*schema.Schema{
Description: "The root storage policy of the instance",
},
"security_group_ids": {
Type: schema.TypeSet,
Type: schema.TypeList,
Computed: true,
Optional: true,
Elem: &schema.Schema{Type: schema.TypeString},
Expand Down
9 changes: 5 additions & 4 deletions fptcloud/security-group-rule/resource_security_group_rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,11 +127,12 @@ func resourceSecurityGroupRuleCreate(ctx context.Context, d *schema.ResourceData
}

if sources, ok := d.GetOk("sources"); ok {
sourceList := sources.([]interface{})
createdModel.Sources = make([]string, len(sourceList))
for i, v := range sourceList {
createdModel.Sources[i] = v.(string)
sourceSet := sources.(*schema.Set)
sourceList := make([]string, 0, len(sourceSet.List()))
for _, v := range sourceSet.List() {
sourceList = append(sourceList, v.(string))
}
createdModel.Sources = sourceList
}

if description, ok := d.GetOk("description"); ok {
Expand Down
23 changes: 13 additions & 10 deletions fptcloud/security-group/resource_security_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,12 @@ func resourceSecurityGroupCreate(ctx context.Context, d *schema.ResourceData, m
}

if applyTo, ok := d.GetOk("apply_to"); ok {
applyToList := applyTo.([]interface{})
createdModel.ApplyTo = make([]string, len(applyToList))
for i, v := range applyToList {
createdModel.ApplyTo[i] = v.(string)
applyToSet := applyTo.(*schema.Set)
applyToList := make([]string, 0, len(applyToSet.List()))
for _, v := range applyToSet.List() {
applyToList = append(applyToList, v.(string))
}
createdModel.ApplyTo = applyToList
}

if okVpcId {
Expand Down Expand Up @@ -161,18 +162,20 @@ func resourceSecurityGroupUpdate(ctx context.Context, d *schema.ResourceData, m
}

if hasChangeApplyTo {
applyToValue, ok := d.Get("apply_to").([]interface{})
applyToValue, ok := d.GetOk("apply_to")
if !ok {
applyToValue = []interface{}{}
applyToValue = &schema.Set{}
}
applyTo := make([]string, len(applyToValue))
for i, v := range applyToValue {
applyTo[i] = v.(string)

applyToSet := applyToValue.(*schema.Set)
applyTo := make([]string, 0, len(applyToSet.List()))
for _, v := range applyToSet.List() {
applyTo = append(applyTo, v.(string))
}

_, err := securityGroupService.UpdateApplyTo(vpcId, d.Id(), applyTo)
if err != nil {
return diag.Errorf("[ERR] An error occurred while change apply to from security group %s: %s", d.Id(), err)
return diag.Errorf("[ERR] An error occurred while changing apply to for security group %s: %s", d.Id(), err)
}

updateStateConf := &retry.StateChangeConf{
Expand Down

0 comments on commit d93d62c

Please sign in to comment.