Skip to content

Commit

Permalink
Merge pull request #27 from vngcloud/iam-vserver
Browse files Browse the repository at this point in the history
Iam vserver
  • Loading branch information
manhtu1997 authored Jul 25, 2024
2 parents 601a7e8 + d594d85 commit 56b67fe
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 84 deletions.
2 changes: 1 addition & 1 deletion client/vks/model_update_node_group_dto.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ package vks

type UpdateNodeGroupDto struct {
AutoScaleConfig *NodeGroupAutoScaleConfigDto `json:"autoScaleConfig,omitempty"`
NumNodes *int32 `json:"numNodes"`
NumNodes *int32 `json:"numNodes,omitempty"`
UpgradeConfig *NodeGroupUpgradeConfigDto `json:"upgradeConfig"`
SecurityGroups []string `json:"securityGroups"`
ImageId string `json:"imageId"`
Expand Down
66 changes: 21 additions & 45 deletions resource/vks/resource_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/vngcloud/terraform-provider-vngcloud/client/vks"
"log"
"net/http"
"reflect"
"time"
)

Expand Down Expand Up @@ -324,6 +325,12 @@ func resourceClusterRead(d *schema.ResourceData, m interface{}) error {
d.Set("enable_private_cluster", cluster.EnablePrivateCluster)
log.Printf("GetConfig\n")
configResp, httpResponse, _ := cli.VksClient.V1ClusterControllerApi.V1ClustersClusterIdKubeconfigGet(context.TODO(), clusterID, nil)
log.Printf("-------------------------------------\n")
log.Printf("status %s\n", string(httpResponse.Status))
log.Printf("-------------------------------------\n")
aaa, _ := json.Marshal(configResp)
log.Printf("config %s\n", string(aaa))

if !CheckErrorResponse(httpResponse) {
log.Printf("SetConfig\n")
d.Set("config", configResp)
Expand All @@ -332,12 +339,6 @@ func resourceClusterRead(d *schema.ResourceData, m interface{}) error {
}

func resourceClusterUpdate(d *schema.ResourceData, m interface{}) error {
if d.HasChange("node_group") {
err := checkRequestNodeGroup(d)
if err != nil {
return err
}
}
if d.HasChange("white_list_node_cidr") || d.HasChange("version") {
err := changeWhiteListNodeOrVersion(d, m)
if err != nil {
Expand All @@ -353,36 +354,6 @@ func resourceClusterUpdate(d *schema.ResourceData, m interface{}) error {
return resourceClusterRead(d, m)
}

func checkRequestNodeGroup(d *schema.ResourceData) error {
nodeGroups := d.Get("node_group").([]interface{})
for _, ng := range nodeGroups {
nodeGroup := ng.(map[string]interface{})
autoScaleConfig := getAutoScaleConfig(nodeGroup["auto_scale_config"].([]interface{}))
var numNodes *int32
if value, ok := nodeGroup["num_nodes"]; ok {
if value != nil {
num := int32(value.(int))
if num != -1 {
numNodes = &num
}
}
}
var err error
if autoScaleConfig != nil && numNodes != nil {
err = fmt.Errorf("If auto_scale_config is set then num_nodes must be -1\n")
}
if autoScaleConfig == nil && numNodes == nil {
err = fmt.Errorf("If auto_scale_config is not set then num_nodes must be different from -1\n")
}
if err != nil {
oldNodeGroup, _ := d.GetChange("node_group")
d.Set("node_group", oldNodeGroup)
return err
}
}
return nil
}

func changeWhiteListNodeOrVersion(d *schema.ResourceData, m interface{}) error {
whiteListCIDRsInterface := d.Get("white_list_node_cidr").([]interface{})
var whiteListCIDR []string
Expand Down Expand Up @@ -432,9 +403,13 @@ func changeWhiteListNodeOrVersion(d *schema.ResourceData, m interface{}) error {
func changeNodeGroup(d *schema.ResourceData, m interface{}) error {
cli := m.(*client.Client)
nodeGroups := d.Get("node_group").([]interface{})
for _, ng := range nodeGroups {
oldNodeGroupSch, _ := d.GetChange("node_group")
oldNodeGroups := oldNodeGroupSch.([]interface{})
for i, ng := range nodeGroups {
if reflect.DeepEqual(ng, oldNodeGroups[i]) {
continue
}
nodeGroup := ng.(map[string]interface{})

securityGroupsRequest := nodeGroup["security_groups"].([]interface{})
var securityGroups []string
for _, s := range securityGroupsRequest {
Expand All @@ -443,14 +418,14 @@ func changeNodeGroup(d *schema.ResourceData, m interface{}) error {
if securityGroups == nil {
securityGroups = make([]string, 0)
}
autoScaleConfig := getAutoScaleConfig(nodeGroup["auto_scale_config"].([]interface{}))
upgradeConfig := getUpgradeConfig(nodeGroup["upgrade_config"].([]interface{}))
var numNodes *int32
if value, ok := nodeGroup["num_nodes"]; ok {
num := int32(value.(int))
if num != -1 {
numNodes = &num
}
oldNodeGroup := oldNodeGroups[i].(map[string]interface{})

autoScaleConfig := getAutoScaleConfig(nodeGroup["auto_scale_config"].([]interface{}))
var numNodes *int32 = nil
if int32(oldNodeGroup["num_nodes"].(int)) != int32(nodeGroup["num_nodes"].(int)) {
num := int32(nodeGroup["num_nodes"].(int))
numNodes = &num
}
imageId := nodeGroup["image_id"].(string)
updateNodeGroupRequest := vks.UpdateNodeGroupDto{
Expand All @@ -465,6 +440,7 @@ func changeNodeGroup(d *schema.ResourceData, m interface{}) error {
}
resp, httpResponse, _ := cli.VksClient.V1NodeGroupControllerApi.V1ClustersClusterIdNodeGroupsNodeGroupIdPut(context.TODO(), d.Id(), nodeGroup["node_group_id"].(string), &requestPutOpts)
if CheckErrorResponse(httpResponse) {
d.Set("node_group", oldNodeGroupSch)
responseBody := GetResponseBody(httpResponse)
errResponse := fmt.Errorf("request fail with errMsg: %s", responseBody)
return errResponse
Expand Down
56 changes: 18 additions & 38 deletions resource/vks/resrouce_cluster_node_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,17 +73,10 @@ var schemaNodeGroup = map[string]*schema.Schema{
ForceNew: true,
},
"num_nodes": {
Type: schema.TypeInt,
Optional: true,
Default: 1,
ValidateFunc: validation.IntAtLeast(-1),
Type: schema.TypeInt,
Optional: true,
Default: 1,
},
//"initial_node_count": {
// Type: schema.TypeInt,
// Optional: true,
// ForceNew: true,
// Default: 1,
//},
"auto_scale_config": {
Type: schema.TypeList,
MaxItems: 1,
Expand Down Expand Up @@ -362,9 +355,6 @@ func resourceClusterNodeGroupRead(d *schema.ResourceData, m interface{}) error {
d.Set("flavor_id", resp.FlavorId)
d.Set("name", resp.Name)
d.Set("ssh_key_id", resp.SshKeyId)
if _, ok := d.GetOkExists("num_nodes"); ok && int32(d.Get("num_nodes").(int)) != -1 {
d.Set("num_nodes", resp.NumNodes)
}
return nil
}

Expand Down Expand Up @@ -507,30 +497,10 @@ func resourceClusterNodeGroupUpdate(d *schema.ResourceData, m interface{}) error
}
autoScaleConfig := getAutoScaleConfig(d.Get("auto_scale_config").([]interface{}))
upgradeConfig := getUpgradeConfig(d.Get("upgrade_config").([]interface{}))
var numNodes *int32
if value, ok := d.GetOkExists("num_nodes"); ok {
num := int32(value.(int))
if num != -1 {
numNodes = &num
}
}
var err error
if autoScaleConfig != nil && numNodes != nil {
err = fmt.Errorf("If auto_scale_config is set then num_nodes must be -1\n")
}
if autoScaleConfig == nil && numNodes == nil {
err = fmt.Errorf("If auto_scale_config is not set then num_nodes must be different from -1\n")
}
if err != nil {
if d.HasChange("auto_scale_config") {
oldAutoScaleConfig, _ := d.GetChange("auto_scale_config")
d.Set("auto_scale_config", oldAutoScaleConfig)
}
if d.HasChange("num_nodes") {
oldNumNodes, _ := d.GetChange("num_nodes")
d.Set("num_nodes", oldNumNodes)
}
return err
var numNodes *int32 = nil
if d.HasChange("num_nodes") {
num := int32(d.Get("num_nodes").(int))
numNodes = &num
}
imageId := d.Get("image_id").(string)
updateNodeGroupRequest := vks.UpdateNodeGroupDto{
Expand All @@ -545,6 +515,16 @@ func resourceClusterNodeGroupUpdate(d *schema.ResourceData, m interface{}) error
}
resp, httpResponse, _ := cli.VksClient.V1NodeGroupControllerApi.V1ClustersClusterIdNodeGroupsNodeGroupIdPut(context.TODO(), clusterId, clusterNodeGroupId, &requestPutOpts)
if CheckErrorResponse(httpResponse) {
autoScaleConfig, _ := d.GetChange("auto_scale_config")
numNodes, _ := d.GetChange("num_nodes")
upgradeConfig, _ := d.GetChange("upgrade_config")
imageId, _ := d.GetChange("image_id")
securityGroups, _ := d.GetChange("security_groups")
d.Set("auto_scale_config", autoScaleConfig)
d.Set("num_nodes", numNodes)
d.Set("upgrade_config", upgradeConfig)
d.Set("image_id", imageId)
d.Set("security_groups", securityGroups)
responseBody := GetResponseBody(httpResponse)
errResponse := fmt.Errorf("request fail with errMsg: %s", responseBody)
return errResponse
Expand All @@ -562,7 +542,7 @@ func resourceClusterNodeGroupUpdate(d *schema.ResourceData, m interface{}) error
Delay: 10 * time.Second,
MinTimeout: 1 * time.Second,
}
_, err = stateConf.WaitForState()
_, err := stateConf.WaitForState()
if err != nil {
return fmt.Errorf("error waiting for update cluster node group (%s) %s", resp.Id, err)
}
Expand Down

0 comments on commit 56b67fe

Please sign in to comment.