func (command *commandDelete) Execute(resource *handler.Resource) { params := resource.Params.(*paramsDelete) containerName := params.container objectName := params.object listOpts := osObjects.ListOpts{ Prefix: objectName, } allPages, err := osObjects.List(command.Ctx.ServiceClient, containerName, listOpts).AllPages() if err != nil { resource.Err = err return } objectNames, err := osObjects.ExtractNames(allPages) if err != nil { resource.Err = err return } for _, thisName := range objectNames { rawResponse := osObjects.Delete(command.Ctx.ServiceClient, containerName, thisName, osObjects.DeleteOpts{}) if rawResponse.Err != nil { resource.Err = rawResponse.Err return } } resource.Result = fmt.Sprintf("Deleted object [%s] from container [%s]", objectName, containerName) }
func (command *commandDeleteMetadata) Execute(resource *handler.Resource) { params := resource.Params.(*paramsDeleteMetadata) containerName := params.containerName objectName := params.objectName currentMetadata, err := objects.Get(command.Ctx.ServiceClient, containerName, objectName, nil).ExtractMetadata() if err != nil { resource.Err = err return } for _, k := range params.metadataKeys { currentMetadata[k] = "" } updateOpts := osObjects.UpdateOpts{ Metadata: currentMetadata, } updateResponse := objects.Update(command.Ctx.ServiceClient, containerName, objectName, updateOpts) if updateResponse.Err != nil { resource.Err = updateResponse.Err return } resource.Result = fmt.Sprintf("Successfully deleted metadata with keys [%s] from object [%s].\n", strings.Join(params.metadataKeys, ", "), objectName) }
func (command *commandRebuild) Execute(resource *handler.Resource) { params := resource.Params.(*paramsRebuild) opts := params.opts serverID := params.serverID server, err := servers.Rebuild(command.Ctx.ServiceClient, serverID, opts).Extract() if err != nil { resource.Err = err return } if params.wait { err = osServers.WaitForStatus(command.Ctx.ServiceClient, serverID, "ACTIVE", 600) if err != nil { resource.Err = err return } server, err = servers.Get(command.Ctx.ServiceClient, serverID).Extract() if err != nil { resource.Err = err return } } resource.Result = serverSingle(server) }
func (command *commandListEvents) Execute(resource *handler.Resource) { params := resource.Params.(*paramsListEvents) opts := params.opts stackName := params.stackName stackID := params.stackID resourceName := params.resourceName pager := stackevents.ListResourceEvents(command.Ctx.ServiceClient, stackName, stackID, resourceName, opts) pages, err := pager.AllPages() if err != nil { resource.Err = err return } info, err := osStackEvents.ExtractResourceEvents(pages) if err != nil { resource.Err = err return } result := make([]map[string]interface{}, len(info)) for j, event := range info { result[j] = structs.Map(&event) result[j]["Time"] = event.Time } resource.Result = result }
func (command *commandSetMetadata) Execute(resource *handler.Resource) { params := resource.Params.(*paramsSetMetadata) currentMetadata, err := accounts.Get(command.Ctx.ServiceClient).ExtractMetadata() if err != nil { resource.Err = err return } i := 0 keys := make([]string, len(currentMetadata)) for k := range currentMetadata { keys[i] = k i++ } updateOpts := osAccounts.UpdateOpts{ Metadata: params.metadata, DeleteMetadata: keys, } updateResponse := accounts.Update(command.Ctx.ServiceClient, updateOpts) if updateResponse.Err != nil { resource.Err = updateResponse.Err return } metadata, err := accounts.Get(command.Ctx.ServiceClient).ExtractMetadata() if err != nil { resource.Err = err return } resource.Result = metadata }
func (command *commandUpdateMetadata) Execute(resource *handler.Resource) { params := resource.Params.(*paramsUpdateMetadata) updateOpts := osAccounts.UpdateOpts{ Metadata: params.metadata, } updateResponse := accounts.Update(command.Ctx.ServiceClient, updateOpts) if updateResponse.Err != nil { resource.Err = updateResponse.Err return } metadata, err := accounts.Get(command.Ctx.ServiceClient).ExtractMetadata() if err != nil { resource.Err = err return } updatedMetadata := make(map[string]string, len(params.metadata)) for k := range params.metadata { k = strings.Title(k) updatedMetadata[k] = metadata[k] } resource.Result = updatedMetadata }
func (command *commandCreate) Execute(resource *handler.Resource) { opts := resource.Params.(*paramsCreate).opts var server *osServers.Server var err error if len(opts.BlockDevice) > 0 { server, err = bfv.Create(command.Ctx.ServiceClient, opts).Extract() } else { server, err = servers.Create(command.Ctx.ServiceClient, opts).Extract() } handleErr: if err != nil { switch err.(type) { case *osServers.ErrNeitherImageIDNorImageNameProvided: err = errors.New("One and only one of the --image-id and the --image-name flags must be provided.") case *osServers.ErrNeitherFlavorIDNorFlavorNameProvided: err = errors.New("One and only one of the --flavor-id and the --flavor-name flags must be provided.") case *gophercloud.ErrErrorAfterReauthentication: err = err.(*gophercloud.ErrErrorAfterReauthentication).UnexpectedResponseCodeError goto handleErr case *gophercloud.UnexpectedResponseCodeError: switch err.(*gophercloud.UnexpectedResponseCodeError).Actual { case 403: imageID := opts.ImageRef if imageID == "" { id, err := osImages.IDFromName(command.Ctx.ServiceClient, opts.ImageName) if err != nil { resource.Err = err return } imageID = id } flavorLabel := "id" flavorID := opts.FlavorRef if flavorID == "" { flavorLabel = "name" flavorID = opts.FlavorName } err = fmt.Errorf(strings.Join([]string{"The flavor you've chosen has a disk size of 0, so an image can't be created on it directly.\n", "To boot with this flavor, creating a 100 GB volume and not deleting that volume when the server is deleted, run this command:\n", fmt.Sprintf("rack servers instance create --name %s --flavor-%s %s \\", opts.Name, flavorLabel, flavorID), fmt.Sprintf("--block-device \"source-type=image,source-id=%s,volume-size=100,destination-type=volume,delete-on-termination=false\"\n", imageID), "For more information please run: rack servers instance create --help", }, "\n")) } } resource.Err = err return } resource.Result = serverSingle(server) }
func (command *commandListAddressesByNetwork) Execute(resource *handler.Resource) { params := resource.Params.(*paramsListAddressesByNetwork) pager := servers.ListAddressesByNetwork(command.Ctx.ServiceClient, params.serverID, params.network) var result []map[string]interface{} err := pager.EachPage(func(page pagination.Page) (bool, error) { addresses, err := servers.ExtractNetworkAddresses(page) if err != nil { return false, err } for _, address := range addresses { m := map[string]interface{}{ "Type": params.network, "Version": address.Version, "Address": address.Address, } result = append(result, m) } return true, nil }) if err != nil { resource.Err = err return } resource.Result = result }
func (command *commandList) Execute(resource *handler.Resource) { opts := resource.Params.(*paramsList).opts pager := osVolumes.List(command.Ctx.ServiceClient, opts) var volumes []map[string]interface{} err := pager.EachPage(func(page pagination.Page) (bool, error) { info, err := osVolumes.ExtractVolumes(page) if err != nil { return false, err } result := make([]map[string]interface{}, len(info)) for j, volume := range info { result[j] = volumeSingle(&volume) } volumes = append(volumes, result...) return true, nil }) if err != nil { resource.Err = err return } if len(volumes) == 0 { resource.Result = nil } else { resource.Result = volumes } }
func (command *commandList) Execute(resource *handler.Resource) { opts := resource.Params.(*paramsList).opts opts.Full = true containerName := resource.Params.(*paramsList).container allPages := resource.Params.(*paramsList).allPages pager := objects.List(command.Ctx.ServiceClient, containerName, opts) if allPages { pages, err := pager.AllPages() if err != nil { resource.Err = err return } info, err := objects.ExtractInfo(pages) if err != nil { resource.Err = err return } result := make([]map[string]interface{}, len(info)) for j, obj := range info { result[j] = structs.Map(&obj) } resource.Result = result } else { limit := opts.Limit err := pager.EachPage(func(page pagination.Page) (bool, error) { info, err := objects.ExtractInfo(page) if err != nil { return false, err } result := make([]map[string]interface{}, len(info)) for j, obj := range info { result[j] = structs.Map(&obj) } resource.Result = result if len(info) >= limit { return false, nil } limit -= len(info) command.Ctx.Results <- resource return true, nil }) if err != nil { resource.Err = err return } } }
func (command *commandGet) Execute(resource *handler.Resource) { buildids, err := buildinfo.Get(command.Ctx.ServiceClient).Extract() if err != nil { resource.Err = err return } resource.Result = structs.Map(buildids) }
func (command *commandDownload) JSON(resource *handler.Resource) { bytes, err := ioutil.ReadAll(resource.Result.(io.Reader)) if err != nil { resource.Err = err return } resource.Result = string(bytes) }
func (command *commandList) Execute(resource *handler.Resource) { opts := resource.Params.(*paramsList).opts allPages := resource.Params.(*paramsList).allPages pager := stacks.List(command.Ctx.ServiceClient, opts) if allPages { pages, err := pager.AllPages() if err != nil { resource.Err = err return } info, err := osStacks.ExtractStacks(pages) if err != nil { resource.Err = err return } result := make([]map[string]interface{}, len(info)) for j, stack := range info { result[j] = stackSingle(&stack) } resource.Result = result } else { limit := opts.Limit err := pager.EachPage(func(page pagination.Page) (bool, error) { info, err := osStacks.ExtractStacks(page) if err != nil { return false, err } result := make([]map[string]interface{}, len(info)) for j, stack := range info { result[j] = stackSingle(&stack) } resource.Result = result if len(info) >= limit { return false, nil } limit -= len(info) command.Ctx.Results <- resource return true, nil }) if err != nil { resource.Err = err return } } }
func (command *commandGet) Execute(resource *handler.Resource) { containerName := resource.Params.(*paramsGet).container containerRaw := containers.Get(command.Ctx.ServiceClient, containerName) containerInfo, err := containerRaw.Extract() if err != nil { resource.Err = err return } containerMetadata, err := containerRaw.ExtractMetadata() if err != nil { resource.Err = err return } resource.Result = structs.Map(containerInfo) resource.Result.(map[string]interface{})["Name"] = containerName resource.Result.(map[string]interface{})["Metadata"] = containerMetadata }
func (command *commandGet) Execute(resource *handler.Resource) { securityGroupRuleID := resource.Params.(*paramsGet).securityGroupRuleID securityGroupRule, err := securityGroupRules.Get(command.Ctx.ServiceClient, securityGroupRuleID).Extract() if err != nil { resource.Err = err return } resource.Result = securityGroupRuleSingle(securityGroupRule) }
func (command *commandCreate) Execute(resource *handler.Resource) { opts := resource.Params.(*paramsCreate).opts volume, err := osVolumes.Create(command.Ctx.ServiceClient, opts).Extract() if err != nil { resource.Err = err return } resource.Result = volumeSingle(volume) }
func (command *commandCreate) Execute(resource *handler.Resource) { createOpts := resource.Params.(*paramsCreate) imageID, err := osServers.CreateImage(command.Ctx.ServiceClient, createOpts.serverID, createOpts.opts).ExtractImageID() if err != nil { resource.Err = err return } resource.Result = map[string]interface{}{"ID": imageID} }
func (command *commandGet) Execute(resource *handler.Resource) { serverID := resource.Params.(*paramsGet).server server, err := servers.Get(command.Context().ServiceClient, serverID).Extract() if err != nil { resource.Err = err return } resource.Result = serverSingle(server) }
func (command *commandUpdateMetadata) Execute(resource *handler.Resource) { params := resource.Params.(*paramsUpdateMetadata) metadata, err := osServers.UpdateMetadata(command.Ctx.ServiceClient, params.serverID, params.opts).Extract() if err != nil { resource.Err = err return } resource.Result = metadata }
func (command *commandDelete) Execute(resource *handler.Resource) { portID := resource.Params.(*paramsDelete).portID err := ports.Delete(command.Ctx.ServiceClient, portID).ExtractErr() if err != nil { resource.Err = err return } resource.Result = fmt.Sprintf("Successfully deleted port [%s]\n", portID) }
func (command *commandGet) Execute(resource *handler.Resource) { subnetID := resource.Params.(*paramsGet).subnetID subnet, err := subnets.Get(command.Ctx.ServiceClient, subnetID).Extract() if err != nil { resource.Err = err return } resource.Result = subnetSingle(subnet) }
func (command *commandGetMetadata) Execute(resource *handler.Resource) { metadata, err := accounts.Get(command.Ctx.ServiceClient).ExtractMetadata() if err != nil { resource.Err = err return } resource.Result = metadata }
func (command *commandUpdate) Execute(resource *handler.Resource) { params := resource.Params.(*paramsUpdate) containerName := params.container containerRaw := containers.Get(command.Ctx.ServiceClient, containerName) if containerRaw.Err != nil { resource.Err = containerRaw.Err return } opts := params.opts rawResponse := containers.Update(command.Ctx.ServiceClient, containerName, opts) if rawResponse.Err != nil { resource.Err = rawResponse.Err return } resource.Result = fmt.Sprintf("Successfully updated container [%s]\n", containerName) }
func (command *commandDelete) Execute(resource *handler.Resource) { securityGroupID := resource.Params.(*paramsDelete).securityGroupID err := securityGroups.Delete(command.Ctx.ServiceClient, securityGroupID).ExtractErr() if err != nil { resource.Err = err return } resource.Result = fmt.Sprintf("Successfully deleted security group [%s]\n", securityGroupID) }
func (command *commandGet) Execute(resource *handler.Resource) { imageID := resource.Params.(*paramsGet).image image, err := images.Get(command.Ctx.ServiceClient, imageID).Extract() if err != nil { resource.Err = err return } resource.Result = structs.Map(image) }
func (command *commandResize) Execute(resource *handler.Resource) { params := resource.Params.(*paramsResize) err := servers.Resize(command.Ctx.ServiceClient, params.serverID, params.opts).ExtractErr() if err != nil { resource.Err = err return } resource.Result = fmt.Sprintf("Successfully resized instance [%s] to flavor [%s]\n", params.serverID, params.opts.FlavorRef) }
func (command *commandUpdate) Execute(resource *handler.Resource) { params := resource.Params.(*paramsUpdate) opts := params.opts stackName := params.stackName stackID := params.stackID err := stacks.Update(command.Ctx.ServiceClient, stackName, stackID, opts).ExtractErr() if err != nil { resource.Err = err return } stack, err := stacks.Get(command.Ctx.ServiceClient, stackName, stackID).Extract() if err != nil { resource.Err = err return } resource.Result = stack }
func (command *commandDelete) Execute(resource *handler.Resource) { serverID := resource.Params.(*paramsDelete).server err := servers.Delete(command.Ctx.ServiceClient, serverID).ExtractErr() if err != nil { resource.Err = err return } resource.Result = fmt.Sprintf("Deleting instance [%s]\n", serverID) }
func (command *commandCreate) Execute(resource *handler.Resource) { opts := resource.Params.(*paramsCreate).opts network, err := networks.Create(command.Ctx.ServiceClient, opts).Extract() if err != nil { resource.Err = err return } resource.Result = networkSingle(network) }
func (command *commandCreate) Execute(resource *handler.Resource) { opts := resource.Params.(*paramsCreate).opts snapshot, err := osSnapshots.Create(command.Ctx.ServiceClient, opts).Extract() if err != nil { resource.Err = err return } resource.Result = snapshotSingle(snapshot) }