MWS Cloud Platform SDK for Go.
⚠️ SDK is under active development and may make breaking changes.
go get go.mws.cloud/go-sdkTo get started, you need to setup project with Go modules and install the MWS Go SDK dependency. This example demonstrates how to list virtual machines inside the project (see runnable example).
Setup Project
mkdir my-mws-project
cd my-mws-project
go mod init my-mws-projectInstall SDK
go get go.mws.cloud/go-sdkWrite Code
Write the following code to main.go file:
package main
import (
"context"
"fmt"
"log"
computeclient "go.mws.cloud/go-sdk/service/compute/client"
computesdk "go.mws.cloud/go-sdk/service/compute/sdk"
"go.mws.cloud/go-sdk/mws"
)
func main() {
ctx := context.Background()
// Use the default loader to load configuration. It will load configuration
// from the environment variables and sensible defaults. You can override
// logic using [mws.LoadSDKOption] functional options. Check the [mws.Load]
// and [mws.Config] for more details.
sdk, err := mws.Load(ctx)
if err != nil {
log.Fatalln("loading sdk error:", err)
}
defer sdk.Close(ctx)
// Create a new virtual machine client using the provided SDK.
client, err := computesdk.NewVirtualMachine(ctx, sdk)
if err != nil {
log.Panicln("creating client error:", err)
}
// List virtual machines with the page size limit.
virtualMachines, err := client.ListVirtualMachines(ctx, computeclient.ListVirtualMachinesRequest{
PageSize: new(10),
})
if err != nil {
log.Panicln("listing virtual machines error:", err)
}
// Print the virtual machine identifiers.
fmt.Println("Virtual Machines:")
for _, vm := range virtualMachines.GetItems() {
fmt.Println(vm.GetMetadata().GetId())
}
}Compile and Run
Before running the code, make sure you have set project and IAM token in the environment variables:
export MWS_PROJECT="your-project"
export MWS_TOKEN="$(mws iam create-token)"
Run the code:
go run main.goOutput:
Virtual Machines:
compute/projects/your-project/virtualMachines/vm-1
compute/projects/your-project/virtualMachines/vm-2
MWS Go SDK requires configuration, like credentials and project identifier. You can provide this information using environment variables and functional options.
MWS_BASE_ENDPOINT- MWS Cloud Platform API base endpoint (default:https://api.mwsapis.ru).MWS_PROJECT- Default project identifier.MWS_ZONE- Default zone identifier (default:ru-central1-a).MWS_TOKEN- IAM token for authentication. If not empty, it will be used in all client requests that require authentication.MWS_SERVICE_ACCOUNT_AUTHORIZED_KEY_PATH- Path to the service account authorized key file used for authentication. Has no effect ifMWS_TOKENis not empty.MWS_TIMEOUT- Timeout for all client requests (default:5s).MWS_LOG_LEVEL- If set, enables SDK operations logging at the specified level. Available levels:debug,info,warn,error,fatal.MWS_TRACE_ENABLED- Enables recording of additional trace data during logging. Trace output may contain sensitive data and is intended for debugging use only.
You can also configure SDK using functional options mws.LoadSDKOption, for example:
sdk, err := mws.Load(ctx,
mws.WithDefaultProject("my-project"),
mws.WithDefaultZone("ru-central1-a"),
mws.WithTimeout(5 * time.Second),
)Note that functional options have highest priority and overrides behavior based on the environment variables and configuration defaults.
MWS Go SDK supports various authentication methods.
You can set IAM token using the MWS_TOKEN environment variable.
Alternatively, this can be done directly in code:
sdk, err := mws.Load(ctx, mws.WithCredentials(
credentials.StaticProvider(credentials.Credentials{
AccessToken: "mws-iam-token",
})
))Please note that the token lifetime is limited.
To authenticate as a service account, you can set the path to its authorized key using the MWS_SERVICE_ACCOUNT_AUTHORIZED_KEY_PATH environment variable.
Alternatively, this can be done directly in code:
key, err := iam.ServiceAccountAuthorizedKeyFromFile("/path/to/key.json")
if err != nil {
log.Panicln("read service account authorized key from file", err)
}
sdk, err := mws.Load(ctx, mws.WithServiceAccountAuthorizedKey(key))You can completely customize authentication by providing your own credentials.Provider implementation:
sdk, err := mws.Load(ctx, mws.WithCredentials(CustomProvider{}))If none of the listed authentication methods is used, SDK will try to detect if the current environment is a Compute VM with an attached service account by performing a request to the instance metadata service. If the request succeeds, SDK will use credentials from the metadata service for authentication.
Check more examples in the examples directory.
Ask for help using the MWS Cloud Platform Support Center.
Created and maintained by MWS Cloud Platform.