This golang library aims to provide an abstraction API of different cloud providers, such as Aliyun and Openstack. Instead of writing code to call different API, this package call those API by code analyzing and reflection, which saves a lot of development work.
Different providers have different utilities and API interfaces to manage their cloud infrastructure, but the abstraction of their interfaces are quiet similar. Using the method of code analyzing and code generation. We format those cloud code into the same pattern, which is easy to unstandstand and use.
For users who want to call the sdk, they only need to provide a json file with details about the cloud resource, operation and parameters, and the underlying function will be called using reflection, the parameters can be find from document of this project and corresponding cloud's document.
For details of this code, see design.
In order to call the cloud sdk api in the same way, we format different cloud code into the same pattern(Std API
). For example, in openstack, there are cloud resources like server and image, and each resource is related to a lot of operation(create, update, delete...). So we generate a function for each operation separately, the function takes a xxxRequest struct as input and a xxxResponse struct as returns.
// action function
func (oc *OpenstackClient) CreateComputeV2Servers(req *CreateComputeV2ServersRequest)(*CreateComputeV2ServersResponse){
return NewCreateComputeV2ServersResponse(servers.Create(oc.Client, req.Opts, ))
}
We offer a command tool to generate both golang and java cloud sdk. You can build the tool with the following command(requires golang > 1.19)
make cloudcodegen
or
go build -o cloudcodegen ./src/code_generator/main.go
Then you can generate code by
cloudcodegen gen -f configFile.json
Here is an example of json config file for aliyun. RegistryConfigs is used to genereate request registry for the
cloud. The CodeGenConfig
include the registry template path(CodeGenConfig
) and the directory path(CodePath
) you
want to save the registry code generated.
{
"CloudType": "Aliyun",
"RegistryConfigs": [
{
"CodeGenConfig": {
"TemplatePath":"",
"CodePath":""
}
}
]
}
- For Openstack, there are two kind of registry(request and response),
- We need to generate new openstack sdk from gophercloud, so the user must provide APICodeConfigs
- The first one is used to generate
Request
code, and the second one is used to generateResult
code. User only need to change the file path and source code path in this config. SourceCodePath
is where you save the gophercloud code in your local file system.- Note that the first TemplatePath of the APICodeConfigs should be openstack_request.tmpl, and the second should be openstack_result.tmpl. Similarly, the
CodeType should be
Request
andResult
.
- The first one is used to generate
{
"CloudType": "Openstack",
"RegistryConfigs": [
{
"CodeGenConfig": {
"TemplatePath": "",
"CodePath": ""
}
}
],
"APICodeConfigs": [
{
"CodeGenConfig": {
"TemplatePath": "",
"CodePath": ""
},
"SourceCodePath": "",
"CodeType": ""
},
{
"CodeGenConfig": {
"TemplatePath": "",
"CodePath": ""
},
"SourceCodePath": "",
"CodeType": ""
}
]
}
We Provide a java-sdk project, which will use this package indirectly with k8s. The code in that package is also generate by this project. The config file is listed as below. User can get the template file from java templates, and the code path need to be kubestack ot the java-sdk project
"JavaCodeConfig": {
"Class": {
"TemplatePath": "",
"CodePath": ""
},
"Domain": {
"TemplatePath": "",
"CodePath": ""
},
"Impl": {
"TemplatePath": "",
"CodePath": ""
},
"LifecycleHeader": {
"TemplatePath": "",
"CodePath": ""
},
"LifecycleClass": {
"TemplatePath": "",
"CodePath": ""
},
"Spec": {
"TemplatePath": "",
"CodePath": ""
}
}
This abstraction of API only accept json format of input. The support action and corresponding prarmeters can be find from the doc of this repository. The detail meaning and correct values of those parameters can be find from those cloud providers' document website.
func main(){
//openstack glance get image
//openstack authentication info
authInfo := map[string]string{
"projectName": "",
"domainName": "",
"identityEndpoint": "",
"username": "",
"password": "",
"Region": "",
"openstackClientType": "image",
"cloudType": "openstack",
}
service, _ := service.NewMultiCloudService(authInfo)
requestByte := `{"Id":"e7db3b45-8db7-47ad-8109-3fb55c2c24fe"}`
resp, err := service.CallCloudAPI("GetImageserviceV2Images", requestByte)
if err != nil {
t.Error(err)
}
}
This example call Aliyun DescribeInstances API.
func main(){
//aliyun authentication info
authInfo := map[string]string{
"regionId":"",
"accessId":"",
"accessKeySecret":"",
"cloudType":"aliyun"
}
mcm, _ := service.NewMultiCloudService(params)
//request json
requestJson := `{
"InstanceIds":"[\"i-2zegiq87g0txkt1bvrb5\"]",
}`
//call underlying cloud API `
ret, err := mcm.CallCloudAPI("DescribeInstances", requestJson)
if err != nil {
t.Error(err)
}
}
cloud | version | document |
---|---|---|
Aliyun | alibaba-cloud-sdk-go(>=1.62.0) | Aliyun ECS |
Openstack | gophercloud | https://docs.openstack.org/zed/api/ |
- support other cloud services