fix
cluster createCluster return
| | |
| | | return true, nil |
| | | } |
| | | |
| | | func CreateCluster(rancherClusterConfig rancher.RancherClusterConfig, clusterName string) error { |
| | | func CreateCluster(rancherClusterConfig rancher.RancherClusterConfig, clusterName string) (string, error) { |
| | | requestBody := createClusterData(clusterName) |
| | | fmt.Println(rancherClusterConfig.RancherURL) |
| | | |
| | | url := fmt.Sprintf("%s/v3/clusters", rancherClusterConfig.RancherURL) |
| | | fmt.Println("url: ", url) |
| | | fmt.Println("body: ", requestBody) |
| | | req, err := http.NewRequest("POST", url, bytes.NewBuffer(requestBody)) |
| | | if err != nil { |
| | | return fmt.Errorf("Failed to create HTTP request: %v", err) |
| | | return "", fmt.Errorf("Failed to create HTTP request: %v", err) |
| | | } |
| | | |
| | | req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", rancherClusterConfig.BearerToken)) |
| | |
| | | |
| | | resp, err := client.Do(req) |
| | | if err != nil { |
| | | return fmt.Errorf("Failed to send HTTP request: %v", err) |
| | | return "", fmt.Errorf("Failed to send HTTP request: %v", err) |
| | | } |
| | | defer resp.Body.Close() |
| | | |
| | | if resp.StatusCode != http.StatusCreated { |
| | | return fmt.Errorf("Failed to create cluster, status code: %d", resp.StatusCode) |
| | | fmt.Println("1111111111111111111111111111 ", resp) |
| | | return "", fmt.Errorf("Failed to create cluster, status code: %d", resp.StatusCode) |
| | | } |
| | | |
| | | var responseBody ClusterCreateResponse |
| | | err = json.NewDecoder(resp.Body).Decode(&responseBody) |
| | | if err != nil { |
| | | return fmt.Errorf("Failed to decode response body: %v", err) |
| | | return "", fmt.Errorf("Failed to decode response body: %v", err) |
| | | } |
| | | |
| | | fmt.Printf("Cluster created: ID=%s, Name=%s\n", responseBody.ID, responseBody.Name) |
| | | |
| | | return nil |
| | | return responseBody.ID, nil |
| | | } |
| | |
| | | clusterID, err := cluster.GetClusterID(rancherClusterConfig.RancherURL, rancherClusterConfig.BearerToken, clusterName) |
| | | if err != nil { |
| | | fmt.Printf("Cluster not found: %s %v\n", clusterName, err) |
| | | err = cluster.CreateCluster(rancherClusterConfig, clusterName) |
| | | _, err = cluster.CreateCluster(rancherClusterConfig, clusterName) |
| | | if err != nil { |
| | | log.Fatalf("Failed to create cluster: %v", err) |
| | | } |