diff --git a/client/vks/model_update_node_group_dto.go b/client/vks/model_update_node_group_dto.go index b49dbf2..0f50631 100644 --- a/client/vks/model_update_node_group_dto.go +++ b/client/vks/model_update_node_group_dto.go @@ -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"` diff --git a/resource/vks/resource_cluster.go b/resource/vks/resource_cluster.go index c37b2ea..21a090f 100644 --- a/resource/vks/resource_cluster.go +++ b/resource/vks/resource_cluster.go @@ -12,6 +12,7 @@ import ( "github.com/vngcloud/terraform-provider-vngcloud/client/vks" "log" "net/http" + "reflect" "time" ) @@ -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) @@ -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 { @@ -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 @@ -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 { @@ -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{ @@ -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 diff --git a/resource/vks/resrouce_cluster_node_group.go b/resource/vks/resrouce_cluster_node_group.go index efd41bd..14517be 100644 --- a/resource/vks/resrouce_cluster_node_group.go +++ b/resource/vks/resrouce_cluster_node_group.go @@ -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, @@ -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 } @@ -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{ @@ -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 @@ -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) }