在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称:gophercloud/gophercloud开源软件地址:https://github.com/gophercloud/gophercloud开源编程语言:Go 99.9%开源软件介绍:Gophercloud: an OpenStack SDK for GoGophercloud is an OpenStack Go SDK. Useful linksHow to installReference a Gophercloud package in your code: import "github.com/gophercloud/gophercloud" Then update your go mod tidy Getting startedCredentialsBecause you'll be hitting an API, you will need to retrieve your OpenStack credentials and either store them as environment variables or in your local Go files. The first method is recommended because it decouples credential information from source code, allowing you to push the latter to your version control system without any security risk. You will need to retrieve the following:
For users that have the OpenStack dashboard installed, there's a shortcut. If
you visit the Authentication
Once you have access to your credentials, you can begin plugging them into Gophercloud. The next step is authentication, and this is handled by a base "Provider" struct. To get one, you can either pass in your credentials explicitly, or tell Gophercloud to use environment variables: import (
"github.com/gophercloud/gophercloud"
"github.com/gophercloud/gophercloud/openstack"
"github.com/gophercloud/gophercloud/openstack/utils"
)
// Option 1: Pass in the values yourself
opts := gophercloud.AuthOptions{
IdentityEndpoint: "https://openstack.example.com:5000/v2.0",
Username: "{username}",
Password: "{password}",
}
// Option 2: Use a utility function to retrieve all your environment variables
opts, err := openstack.AuthOptionsFromEnv() Once you have the provider, err := openstack.AuthenticatedClient(opts) The Provision a serverOnce we have a base Provider, we inject it as a dependency into each OpenStack service. In order to work with the Compute API, we need a Compute service client; which can be created like so: client, err := openstack.NewComputeV2(provider, gophercloud.EndpointOpts{
Region: os.Getenv("OS_REGION_NAME"),
}) We then use this import "github.com/gophercloud/gophercloud/openstack/compute/v2/servers"
server, err := servers.Create(client, servers.CreateOpts{
Name: "My new server!",
FlavorRef: "flavor_id",
ImageRef: "image_id",
}).Extract() The above code sample creates a new server with the parameters, and embodies the
new resource in the Advanced UsageHave a look at the FAQ for some tips on customizing the way Gophercloud works. Backwards-Compatibility GuaranteesNone. Vendor it and write tests covering the parts you use. ContributingSee the contributing guide. Help and feedbackIf you're struggling with something or have spotted a potential bug, feel free to submit an issue to our bug tracker. Thank YouWe'd like to extend special thanks and appreciation to the following: OpenLabOpenLab is providing a full CI environment to test each PR and merge for a variety of OpenStack releases. VEXXHOSTVEXXHOST is providing their services to assist with the development and testing of Gophercloud. |
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论