wangpengfei
2023-05-31 052db984ad73ddb0ba66237e93a48b5e120e6a4e
src/cluster/cluster.go
@@ -8,8 +8,8 @@
   "net/http"
   "strings"
   "basic.com/aps/aps_deploy.git/src/util"
   "basic.com/aps/aps_deploy.git/src/rancher"
   "basic.com/aps/aps_deploy.git/src/util"
)
type Cluster struct {
@@ -146,6 +146,46 @@
   return "", fmt.Errorf("cluster '%s' not found", clusterName)
}
func GetClusters(serverURL, bearerToken string) ([]string, error) {
   url := fmt.Sprintf("%s/v3/clusters", serverURL)
   req, err := http.NewRequest("GET", url, nil)
   if err != nil {
      return nil, fmt.Errorf("failed to get cluster list request: %v", err)
   }
   req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", bearerToken))
   client := util.CreateHTTPClient()
   resp, err := client.Do(req)
   if err != nil {
      return nil, fmt.Errorf("failed to get cluster list: %v", err)
   }
   defer resp.Body.Close()
   if resp.StatusCode != http.StatusOK {
      return nil, fmt.Errorf("failed to get cluster list: unexpected status code %d", resp.StatusCode)
   }
   // Parse the API response
   var clustersResponse ClustersResponse
   err = json.NewDecoder(resp.Body).Decode(&clustersResponse)
   if err != nil {
      return nil, fmt.Errorf("failed to decode cluster list response: %v", err)
   }
   var clusters = make([]string, 0)
   // Print cluster names
   for _, cluster := range clustersResponse.Data {
      clusters = append(clusters, cluster.ID)
      fmt.Printf("Cluster ID: %s, Name: %s\n", cluster.ID, cluster.Name)
   }
   return clusters, nil
}
type Node struct {
   ClusterName string   `json:"clusterName"`
   Roles       []string `json:"roles"`
@@ -154,7 +194,6 @@
   SSHPassword string   `json:"sshPassword"`
   SSHPort     int      `json:"sshPort"`
}
type ClusterCreateRequest struct {
   Name                          string      `json:"name"`
@@ -249,7 +288,7 @@
func CreateCluster(rancherClusterConfig rancher.RancherClusterConfig, clusterName string) error {
   requestBody := createClusterData(clusterName)
        fmt.Println(rancherClusterConfig.RancherURL)
   fmt.Println(rancherClusterConfig.RancherURL)
   url := fmt.Sprintf("%s/v3/clusters", rancherClusterConfig.RancherURL)
   req, err := http.NewRequest("POST", url, bytes.NewBuffer(requestBody))