Skip to content

Instantly share code, notes, and snippets.

@hongchaodeng
Last active April 23, 2020 18:27
Show Gist options
  • Save hongchaodeng/9bfca8e94366a7c93964d6fd64926a5b to your computer and use it in GitHub Desktop.
Save hongchaodeng/9bfca8e94366a7c93964d6fd64926a5b to your computer and use it in GitHub Desktop.
使用 alibaba-cloud-sdk-go 创建 ROS 资源
package main
import (
"fmt"
"github.com/aliyun/alibaba-cloud-sdk-go/sdk"
"github.com/aliyun/alibaba-cloud-sdk-go/sdk/requests"
)
var (
templateBody = ```
{
"ROSTemplateFormatVersion": "2015-09-01",
"Parameters": {
"Name": {
"Type": "String"
},
"MasterInstanceTypes": {
"Type": "Json",
"Default": [
"ecs.c5.large",
"ecs.c5.large",
"ecs.c5.large"
]
},
"WorkerInstanceTypes": {
"Type": "Json",
"Default": [
"ecs.c5.large"
]
},
"LoginPassword": {
"Type": "String"
}
},
"Resources": {
"Vpc": {
"Type": "ALIYUN::ECS::VPC",
"Properties": {
"CidrBlock": "192.168.0.0/16"
}
},
"VSwitch1": {
"Type": "ALIYUN::ECS::VSwitch",
"Properties": {
"VpcId": {
"Ref": "Vpc"
},
"ZoneId": {
"Fn::Select": [
"0",
{
"Fn::GetAZs": {
"Ref": "ALIYUN::Region"
}
}
]
},
"CidrBlock": "192.168.1.0/24"
}
},
"VSwitch2": {
"Type": "ALIYUN::ECS::VSwitch",
"Properties": {
"VpcId": {
"Ref": "Vpc"
},
"ZoneId": {
"Fn::Select": [
"0",
{
"Fn::GetAZs": {
"Ref": "ALIYUN::Region"
}
}
]
},
"CidrBlock": "192.168.2.0/24"
}
},
"VSwitch3": {
"Type": "ALIYUN::ECS::VSwitch",
"Properties": {
"VpcId": {
"Ref": "Vpc"
},
"ZoneId": {
"Fn::Select": [
"0",
{
"Fn::GetAZs": {
"Ref": "ALIYUN::Region"
}
}
]
},
"CidrBlock": "192.168.0.0/24"
}
},
"KubernetesCluster": {
"Type": "ALIYUN::CS::KubernetesCluster",
"Properties": {
"MasterInstanceTypes": {
"Ref": "MasterInstanceTypes"
},
"WorkerVSwitchIds": [
{
"Ref": "VSwitch1"
}
],
"VpcId": {
"Ref": "Vpc"
},
"WorkerInstanceTypes": {
"Ref": "WorkerInstanceTypes"
},
"Name": {
"Ref": "Name"
},
"MasterVSwitchIds": [
{
"Ref": "VSwitch1"
},
{
"Ref": "VSwitch2"
},
{
"Ref": "VSwitch3"
}
],
"LoginPassword": {
"Ref": "LoginPassword"
}
}
}
},
"Outputs": {
"ClusterId": {
"Value": {
"Fn::GetAtt": [
"KubernetesCluster",
"ClusterId"
]
}
},
"TaskId": {
"Value": {
"Fn::GetAtt": [
"KubernetesCluster",
"TaskId"
]
}
}
}
}
```
)
func main() {
client, err := sdk.NewClientWithAccessKey("us-west-1", "<accessKeyId>", "<accessSecret>")
if err != nil {
panic(err)
}
request := requests.NewCommonRequest()
request.Method = "POST"
request.Scheme = "https" // https | http
request.Domain = "ros.aliyuncs.com"
request.Version = "2019-09-10"
request.ApiName = "CreateStack"
request.QueryParams["RegionId"] = "us-west-1"
request.QueryParams["StackName"] = "test_k8s"
request.QueryParams["TemplateBody"] = fmt.Sprintf("%q", templateBody)
request.QueryParams["Parameters.1.ParameterKey"] = "Name"
request.QueryParams["Parameters.1.ParameterValue"] = "admin"
request.QueryParams["Parameters.2.ParameterKey"] = "MasterInstanceTypes"
request.QueryParams["Parameters.2.ParameterValue"] = "[\"ecs.c5.large\",\"ecs.c5.large\",\"ecs.c5.large\"]"
request.QueryParams["Parameters.3.ParameterKey"] = "WorkerInstanceTypes"
request.QueryParams["Parameters.3.ParameterValue"] = "[\"ecs.c5.large\"]"
request.QueryParams["Parameters.4.ParameterKey"] = "LoginPassword"
request.QueryParams["Parameters.4.ParameterValue"] = "admin123"
response, err := client.ProcessCommonRequest(request)
if err != nil {
panic(err)
}
fmt.Print(response.GetHttpContentString())
}
package main
import (
"context"
"fmt"
"github.com/crossplane/provider-alibaba/apis/database/v1alpha1"
"github.com/crossplane/provider-alibaba/pkg/clients/rds"
)
func main() {
cli, err := rds.NewClient(context.TODO(), "xxx", "xxx", "cn-hangzhou")
if err != nil {
panic(err)
}
delete := false
if !delete {
d, err := cli.CreateDBInstance(rds.MakeCreateDBInstanceRequest("test-instance", &v1alpha1.RDSInstanceParameters{
Engine: v1alpha1.PostgresqlEngine,
EngineVersion: "10.0",
DBInstanceClass: "rds.pg.s1.small",
DBInstanceStorageInGB: 20,
SecurityIPList: "0.0.0.0/0",
}))
if err != nil {
panic(err)
}
fmt.Println("id", d.ID, "addr", d.Endpoint.Address, "port", d.Endpoint.Port)
} else {
err = cli.DeleteDBInstance("test-instance")
if err != nil {
panic(err)
}
}
}
@hongchaodeng
Copy link
Author

{
  "ROSTemplateFormatVersion": "2015-09-01",
  "Parameters": {
    "Name": {
      "Type": "String"
    },
    "MasterInstanceTypes": {
      "Type": "Json",
      "Default": [
        "ecs.c5.large",
        "ecs.c5.large",
        "ecs.c5.large"
      ]
    },
    "WorkerInstanceTypes": {
      "Type": "Json",
      "Default": [
        "ecs.c5.large"
      ]
    },
    "LoginPassword": {
      "Type": "String"
    }
  },
  "Resources": {
    "Vpc": {
      "Type": "ALIYUN::ECS::VPC",
      "Properties": {
        "CidrBlock": "192.168.0.0/16"
      }
    },
    "VSwitch1": {
      "Type": "ALIYUN::ECS::VSwitch",
      "Properties": {
        "VpcId": {
          "Ref": "Vpc"
        },
        "ZoneId": {
          "Fn::Select": [
            "0",
            {
              "Fn::GetAZs": {
                "Ref": "ALIYUN::Region"
              }
            }
          ]
        },
        "CidrBlock": "192.168.1.0/24"
      }
    },
    "VSwitch2": {
      "Type": "ALIYUN::ECS::VSwitch",
      "Properties": {
        "VpcId": {
          "Ref": "Vpc"
        },
        "ZoneId": {
          "Fn::Select": [
            "0",
            {
              "Fn::GetAZs": {
                "Ref": "ALIYUN::Region"
              }
            }
          ]
        },
        "CidrBlock": "192.168.2.0/24"
      }
    },
    "VSwitch3": {
      "Type": "ALIYUN::ECS::VSwitch",
      "Properties": {
        "VpcId": {
          "Ref": "Vpc"
        },
        "ZoneId": {
          "Fn::Select": [
            "0",
            {
              "Fn::GetAZs": {
                "Ref": "ALIYUN::Region"
              }
            }
          ]
        },
        "CidrBlock": "192.168.0.0/24"
      }
    },
    "KubernetesCluster": {
      "Type": "ALIYUN::CS::KubernetesCluster",
      "Properties": {
        "MasterInstanceTypes": {
          "Ref": "MasterInstanceTypes"
        },
        "WorkerVSwitchIds": [
          {
            "Ref": "VSwitch1"
          }
        ],
        "VpcId": {
          "Ref": "Vpc"
        },
        "WorkerInstanceTypes": {
          "Ref": "WorkerInstanceTypes"
        },
        "Name": {
          "Ref": "Name"
        },
        "MasterVSwitchIds": [
          {
            "Ref": "VSwitch1"
          },
          {
            "Ref": "VSwitch2"
          },
          {
            "Ref": "VSwitch3"
          }
        ],
        "LoginPassword": {
          "Ref": "LoginPassword"
        }
      }
    }
  },
  "Outputs": {
    "ClusterId": {
      "Value": {
        "Fn::GetAtt": [
          "KubernetesCluster",
          "ClusterId"
        ]
      }
    },
    "TaskId": {
      "Value": {
        "Fn::GetAtt": [
          "KubernetesCluster",
          "TaskId"
        ]
      }
    }
  }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment