Exemplo n.º 1
0
func (sdk *RackspaceSDK) GetAllFlavor() []flavors.Flavor {
	out := []flavors.Flavor{}
	err := flavors.ListDetail(sdk.p, nil).EachPage(func(p pagination.Page) (quit bool, err error) {
		v, err := flavors.ExtractFlavors(p)
		out = append(out, v...)
		return true, err
	})
	handleErr(err)
	return out
}
Exemplo n.º 2
0
// 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)
	}
}
Exemplo n.º 3
0
Arquivo: list.go Projeto: flazz/rack
func (command *commandList) Execute(resource *handler.Resource) {
	opts := resource.Params.(*paramsList).opts
	allPages := resource.Params.(*paramsList).allPages
	pager := flavors.ListDetail(command.Ctx.ServiceClient, opts)
	if allPages {
		pages, err := pager.AllPages()
		if err != nil {
			resource.Err = err
			return
		}
		info, err := flavors.ExtractFlavors(pages)
		if err != nil {
			resource.Err = err
			return
		}
		result := make([]map[string]interface{}, len(info))
		for j, flavor := range info {
			result[j] = structs.Map(flavor)
		}
		resource.Result = result
	} else {
		limit := opts.Limit
		err := pager.EachPage(func(page pagination.Page) (bool, error) {
			info, err := flavors.ExtractFlavors(page)
			if err != nil {
				return false, err
			}
			result := make([]map[string]interface{}, len(info))
			for j, flavor := range info {
				result[j] = structs.Map(flavor)
			}
			resource.Result = result
			if len(info) >= opts.Limit {
				return false, nil
			}
			limit -= len(info)
			command.Ctx.WaitGroup.Add(1)
			command.Ctx.Results <- resource
			return true, nil
		})
		if err != nil {
			resource.Err = err
			return
		}
	}
}
Exemplo n.º 4
0
// 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
}