func NewRackspaceSDK(username, apiKey, SSHKeyName string) *RackspaceSDK { if SSHKeyName == "" { panic("Empty SSHKeyName of instance don't allow!") } sdk := &RackspaceSDK{ Username: username, APIKey: apiKey, SSHKeyName: SSHKeyName, Region: "HKG", InstanceName: "kmg-RackspaceSDK-auto-build", ImageName: "Ubuntu 14.04 LTS (Trusty Tahr) (PVHVM)", FlavorName: "4 GB General Purpose v1", } ao := gophercloud.AuthOptions{ Username: sdk.Username, APIKey: sdk.APIKey, } provider, err := rackspace.AuthenticatedClient(ao) handleErr(err) serviceClient, err := rackspace.NewComputeV2(provider, gophercloud.EndpointOpts{ Region: sdk.Region, }) handleErr(err) sdk.p = serviceClient return sdk }
func newClient() (*gophercloud.ServiceClient, error) { // Obtain credentials from the environment. options, err := rackspace.AuthOptionsFromEnv() if err != nil { return nil, err } options = tools.OnlyRS(options) region := os.Getenv("RS_REGION") if options.Username == "" { return nil, errors.New("Please provide a Rackspace username as RS_USERNAME.") } if options.APIKey == "" { return nil, errors.New("Please provide a Rackspace API key as RS_API_KEY.") } if region == "" { return nil, errors.New("Please provide a Rackspace region as RS_REGION.") } client, err := rackspace.AuthenticatedClient(options) if err != nil { return nil, err } return rackspace.NewComputeV2(client, gophercloud.EndpointOpts{ Region: region, }) }
func GetList(c *cli.Context) { // assign vars from cli args user := c.String("user") key := c.String("key") region := c.String("region") // step 1, set up auth options ao := gophercloud.AuthOptions{ Username: user, APIKey: key, } // step 2, rax auth to get back provider instance provider, err := rackspace.AuthenticatedClient(ao) if err != nil { fmt.Println(err) } // set rax region serviceClient, err2 := rackspace.NewComputeV2(provider, gophercloud.EndpointOpts{ Region: region, }) if err2 != nil { fmt.Println(err2) } // Retrieve a pager (i.e. a paginated collection) server_pager := servers.List(serviceClient, nil) // Define an anonymous function to be executed on each page's iteration err6 := server_pager.EachPage(func(page pagination.Page) (bool, error) { serverList, err7 := servers.ExtractServers(page) if err7 != nil { fmt.Println(err7) } for _, s := range serverList { // "s" will be a servers.Server // https://github.com/rackspace/gophercloud/blob/master/openstack/compute/v2/servers/results.go fmt.Println("Name: ", s.Name) fmt.Println("UUID: ", s.ID) fmt.Println("Created: ", s.Created) fmt.Println("Updated: ", s.Updated) fmt.Println("Progress: ", s.Progress) fmt.Println("HostID: ", s.HostID) fmt.Println("Image: ", s.Image["id"]) fmt.Println("Flavor ID: ", s.Flavor["id"]) fmt.Println("Status: ", s.Status) fmt.Println("Access: ", s.AccessIPv4) fmt.Println("Public: ", s.Addresses["public"]) fmt.Println("Addresses: ", s.Addresses) fmt.Println("\n") } return true, nil }) if err6 != nil { fmt.Println(err6) } }
func (rs *Rackspace) getComputeClient() (*gophercloud.ServiceClient, error) { client, err := rackspace.NewComputeV2(rs.provider, gophercloud.EndpointOpts{ Region: rs.region, }) if err != nil || client == nil { glog.Errorf("Unable to initialize nova client for region: %s", rs.region) } return client, nil }
func newComputeClient() (*gophercloud.ServiceClient, error) { provider, err := newProvider() if err != nil { return nil, err } return rackspace.NewComputeV2(provider, gophercloud.EndpointOpts{ Region: os.Getenv("RS_REGION"), }) }
// Instances returns an implementation of Instances for Rackspace. func (os *Rackspace) Instances() (cloudprovider.Instances, bool) { glog.V(2).Info("rackspace.Instances() called") compute, err := rackspace.NewComputeV2(os.provider, gophercloud.EndpointOpts{ Region: os.region, }) if err != nil { glog.Warningf("Failed to find compute endpoint: %v", err) return nil, false } glog.V(1).Info("Claiming to support Instances") return &Instances{compute}, true }
// print list of cloud servers images to stdout func Get(c *cli.Context) { // assign vars from cli args user := c.String("user") key := c.String("key") region := c.String("region") // step 1, set up auth options ao := gophercloud.AuthOptions{ Username: user, APIKey: key, } // step 2, rax auth to get back provider instance provider, err := rackspace.AuthenticatedClient(ao) if err != nil { fmt.Println(err) } // set rax region serviceClient, err := rackspace.NewComputeV2(provider, gophercloud.EndpointOpts{ Region: region, }) if err != nil { fmt.Println(err) } //opts3 := images.ListOpts{} cmdErr := images.ListDetail(serviceClient, nil).EachPage(func(page pagination.Page) (bool, error) { images, err := images.ExtractImages(page) if err != nil { fmt.Println(err) } // Use the page of []images.Image // https://github.com/rackspace/gophercloud/blob/master/openstack/compute/v2/images/results.go for _, i := range images { fmt.Println("Name: ", i.Name) fmt.Println("ID: ", i.ID) fmt.Println("Created: ", i.Created) fmt.Println("Updated: ", i.Updated) fmt.Println("MinDisk: ", i.MinDisk) fmt.Println("MinRAM: ", i.MinRAM) fmt.Println("Progress: ", i.Progress) fmt.Println("Status: ", i.Status) fmt.Println("\n") } return true, nil }) if cmdErr != nil { fmt.Println(cmdErr) } }
// print details of cloud servers instance to stdout func Details(c *cli.Context) { // assign vars from cli args user := c.String("user") key := c.String("key") region := c.String("region") // server create specific options // check both options and arguments for server uuid serverid := c.Args().First() if c.String("uuid") != "" { serverid = c.String("uuid") } // step 1, set up auth options ao := gophercloud.AuthOptions{ Username: user, APIKey: key, } // step 2, rax auth to get back provider instance provider, err := rackspace.AuthenticatedClient(ao) if err != nil { fmt.Println(err) } // set rax region serviceClient, err2 := rackspace.NewComputeV2(provider, gophercloud.EndpointOpts{ Region: region, }) if err2 != nil { fmt.Println(err2) } // get cloud server s, id := servers.Get(serviceClient, serverid).Extract() fmt.Println("Name: ", s.Name) fmt.Println("UUID: ", s.ID) fmt.Println("Created: ", s.Created) fmt.Println("Updated: ", s.Updated) fmt.Println("Progress: ", s.Progress) fmt.Println("HostID: ", s.HostID) fmt.Println("Image: ", s.Image["id"]) fmt.Println("Flavor ID: ", s.Flavor["id"]) fmt.Println("Status: ", s.Status) fmt.Println("Access: ", s.AccessIPv4) fmt.Println("Public: ", s.Addresses["public"]) fmt.Println("Addresses: ", s.Addresses) if id != nil { fmt.Println(id) } }
// print list of cloud servers flavors to stdout func Get(c *cli.Context) { // assign vars from cli args user := c.String("user") key := c.String("key") region := c.String("region") // step 1, set up auth options ao := gophercloud.AuthOptions{ Username: user, APIKey: key, } // step 2, rax auth to get back provider instance provider, err := rackspace.AuthenticatedClient(ao) if err != nil { fmt.Println(err) } // set rax region serviceClient, err := rackspace.NewComputeV2(provider, gophercloud.EndpointOpts{ Region: region, }) if err != nil { fmt.Println(err) } opts := flavors.ListOpts{} cmdErr := flavors.ListDetail(serviceClient, opts).EachPage(func(page pagination.Page) (bool, error) { flavors, err := flavors.ExtractFlavors(page) if err != nil { fmt.Println(err) } // Use the page of []flavors.Flavor // https://github.com/rackspace/gophercloud/blob/master/openstack/compute/v2/flavors/results.go for _, f := range flavors { fmt.Println("ID: ", f.ID) fmt.Println("Name: ", f.Name) fmt.Println("Disk: ", f.Disk) fmt.Println("RAM: ", f.RAM) fmt.Println("VCPUs: ", f.VCPUs) fmt.Println("\n") } return true, nil }) if cmdErr != nil { fmt.Println(cmdErr) } }
// Instances returns an implementation of Instances for Rackspace. func (os *Rackspace) Instances() (cloudprovider.Instances, bool) { glog.V(2).Info("rackspace.Instances() called") compute, err := rackspace.NewComputeV2(os.provider, gophercloud.EndpointOpts{ Region: os.region, }) if err != nil { glog.Warningf("Failed to find compute endpoint: %v", err) return nil, false } pager := flavors.ListDetail(compute, nil) flavor_to_resource := make(map[string]*api.NodeResources) err = pager.EachPage(func(page pagination.Page) (bool, error) { flavorList, err := flavors.ExtractFlavors(page) if err != nil { return false, err } for _, flavor := range flavorList { rsrc := api.NodeResources{ Capacity: api.ResourceList{ api.ResourceCPU: *resource.NewMilliQuantity(int64(flavor.VCPUs*1000), resource.DecimalSI), api.ResourceMemory: resource.MustParse(fmt.Sprintf("%dMi", flavor.RAM)), "openstack.org/disk": resource.MustParse(fmt.Sprintf("%dG", flavor.Disk)), "openstack.org/rxTxFactor": *resource.NewQuantity(int64(flavor.RxTxFactor*1000), resource.DecimalSI), "openstack.org/swap": resource.MustParse(fmt.Sprintf("%dMi", flavor.Swap)), }, } flavor_to_resource[flavor.ID] = &rsrc } return true, nil }) if err != nil { glog.Warningf("Failed to find compute flavors: %v", err) return nil, false } glog.V(2).Infof("Found %v compute flavors", len(flavor_to_resource)) glog.V(1).Info("Claiming to support Instances") return &Instances{compute, flavor_to_resource}, true }
// create cloud servers instance and print out results to stdout (eg. password) func Create(c *cli.Context) { // assign vars from cli args user := c.String("user") key := c.String("key") region := c.String("region") // server create specific options newservername := c.String("name") imageid := c.String("image") flavorid := c.String("flavor") // step 1, set up auth options ao := gophercloud.AuthOptions{ Username: user, APIKey: key, } // step 2, rax auth to get back provider instance provider, err := rackspace.AuthenticatedClient(ao) if err != nil { fmt.Println(err) } // set rax region serviceClient, err2 := rackspace.NewComputeV2(provider, gophercloud.EndpointOpts{ Region: region, }) if err2 != nil { fmt.Println(err2) } // create the cloud server server, err3 := servers.Create(serviceClient, servers.CreateOpts{ Name: newservername, ImageRef: imageid, FlavorRef: flavorid, }).Extract() fmt.Println(server) if err3 != nil { fmt.Println(err3) } }
func authFromScratch(ao gophercloud.AuthOptions, region, serviceType string) (*gophercloud.ServiceClient, error) { pc, err := rackspace.AuthenticatedClient(ao) if err != nil { return nil, err } var sc *gophercloud.ServiceClient switch serviceType { case "compute": sc, err = rackspace.NewComputeV2(pc, gophercloud.EndpointOpts{ Region: region, }) break case "object-store": sc, err = rackspace.NewObjectStorageV1(pc, gophercloud.EndpointOpts{ Region: region, }) break case "blockstorage": sc, err = rackspace.NewBlockStorageV1(pc, gophercloud.EndpointOpts{ Region: region, }) break case "network": sc, err = rackspace.NewNetworkV2(pc, gophercloud.EndpointOpts{ Region: region, }) break } if err != nil { return nil, err } if sc == nil { return nil, fmt.Errorf("Unable to create service client: Unknown service type: %s", serviceType) } sc.UserAgent.Prepend(util.UserAgent) return sc, nil }