Esempio n. 1
0
func (command *commandUpdateMetadata) Execute(resource *handler.Resource) {
	params := resource.Params.(*paramsUpdateMetadata)
	containerName := params.containerName

	getResponse := containers.Get(command.Ctx.ServiceClient, containerName)
	if getResponse.Err != nil {
		resource.Err = getResponse.Err
		return
	}

	updateOpts := containers.UpdateOpts{
		Metadata: params.metadata,
	}
	updateResponse := containers.Update(command.Ctx.ServiceClient, containerName, updateOpts)
	if updateResponse.Err != nil {
		resource.Err = updateResponse.Err
		return
	}

	metadata, err := containers.Get(command.Ctx.ServiceClient, containerName).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
}
Esempio n. 2
0
func (command *commandUpload) Execute(resource *handler.Resource) {
	params := resource.Params.(*paramsUpload)
	containerName := params.container
	objectName := params.object
	opts := params.opts
	var readSeeker io.ReadSeeker
	var err error
	if stream := params.stream; stream {
		readSeeker = os.Stdin
	} else if fileName := params.fileName; fileName != "" {
		// this file will be closed by Gophercloud, if not closed before then
		readSeeker, err = os.Open(fileName)
		if err != nil {
			resource.Err = err
			return
		}
	} else {
		content := params.content
		readSeeker = strings.NewReader(content)
	}
	rawResponse := objects.Create(command.Ctx.ServiceClient, containerName, objectName, readSeeker, opts)
	if rawResponse.Err != nil {
		resource.Err = rawResponse.Err
		return
	}
	resource.Result = fmt.Sprintf("Successfully uploaded object [%s] to container [%s]\n", objectName, containerName)
}
Esempio n. 3
0
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)
}
Esempio n. 4
0
func (command *commandSetMetadata) Execute(resource *handler.Resource) {
	params := resource.Params.(*paramsSetMetadata)
	containerName := params.containerName

	currentMetadata, err := containers.Get(command.Ctx.ServiceClient, containerName).ExtractMetadata()
	if err != nil {
		resource.Err = err
		return
	}

	i := 0
	keys := make([]string, len(currentMetadata))
	for k := range currentMetadata {
		keys[i] = k
		i++
	}

	updateOpts := containers.UpdateOpts{
		Metadata:       params.metadata,
		DeleteMetadata: keys,
	}
	updateResponse := containers.Update(command.Ctx.ServiceClient, containerName, updateOpts)
	if updateResponse.Err != nil {
		resource.Err = updateResponse.Err
		return
	}

	metadata, err := containers.Get(command.Ctx.ServiceClient, containerName).ExtractMetadata()
	if err != nil {
		resource.Err = err
		return
	}

	resource.Result = metadata
}
Esempio n. 5
0
File: list.go Progetto: hdansou/rack
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.WaitGroup.Add(1)
			command.Ctx.Results <- resource
			return true, nil
		})
		if err != nil {
			resource.Err = err
			return
		}
	}
}
Esempio n. 6
0
func (command *commandListAddresses) Execute(resource *handler.Resource) {
	serverID := resource.Params.(*paramsListAddresses).serverID
	pager := servers.ListAddresses(command.Ctx.ServiceClient, serverID)
	var result []map[string]interface{}
	err := pager.EachPage(func(page pagination.Page) (bool, error) {
		addressesMap, err := servers.ExtractAddresses(page)
		if err != nil {
			return false, err
		}
		for t, addresses := range addressesMap {
			for _, address := range addresses {
				m := map[string]interface{}{
					"Type":    t,
					"Version": address.Version,
					"Address": address.Address,
				}
				result = append(result, m)
			}
		}
		return true, nil
	})
	if err != nil {
		resource.Err = err
		return
	}
	resource.Result = result
}
Esempio n. 7
0
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
	}
}
Esempio n. 8
0
File: list.go Progetto: flazz/rack
func (command *commandList) Execute(resource *handler.Resource) {
	opts := resource.Params.(*paramsList).opts
	allPages := resource.Params.(*paramsList).allPages
	pager := subnets.List(command.Ctx.ServiceClient, opts)
	if allPages {
		pages, err := pager.AllPages()
		if err != nil {
			resource.Err = err
			return
		}
		info, err := osSubnets.ExtractSubnets(pages)
		if err != nil {
			resource.Err = err
			return
		}
		result := make([]map[string]interface{}, len(info))
		for j, subnet := range info {
			result[j] = subnetSingle(&subnet)
		}
		resource.Result = result
	} else {
		limit := opts.Limit
		err := pager.EachPage(func(page pagination.Page) (bool, error) {
			info, err := osSubnets.ExtractSubnets(page)
			if err != nil {
				return false, err
			}
			result := make([]map[string]interface{}, len(info))
			for j, subnet := range info {
				result[j] = subnetSingle(&subnet)
			}
			resource.Result = result
			if len(info) >= 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
		}
	}
}
Esempio n. 9
0
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
}
Esempio n. 10
0
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)
}
Esempio n. 11
0
func (command *commandDelete) Execute(resource *handler.Resource) {
	snapshotID := resource.Params.(*paramsDelete).snapshotID
	err := osSnapshots.Delete(command.Ctx.ServiceClient, snapshotID).ExtractErr()
	if err != nil {
		resource.Err = err
		return
	}
	resource.Result = fmt.Sprintf("Deleting snapshot [%s]\n", snapshotID)
}
Esempio n. 12
0
File: get.go Progetto: hdansou/rack
func (command *commandGet) Execute(resource *handler.Resource) {
	portID := resource.Params.(*paramsGet).portID
	port, err := ports.Get(command.Ctx.ServiceClient, portID).Extract()
	if err != nil {
		resource.Err = err
		return
	}
	resource.Result = portSingle(port)
}
Esempio n. 13
0
File: get.go Progetto: flazz/rack
func (command *commandGet) Execute(resource *handler.Resource) {
	securityGroupID := resource.Params.(*paramsGet).securityGroupID
	securityGroup, err := securityGroups.Get(command.Ctx.ServiceClient, securityGroupID).Extract()
	if err != nil {
		resource.Err = err
		return
	}
	resource.Result = securityGroupSingle(securityGroup)
}
Esempio n. 14
0
func (command *commandCreate) Execute(resource *handler.Resource) {
	opts := resource.Params.(*paramsCreate).opts
	securityGroup, err := securityGroups.Create(command.Ctx.ServiceClient, *opts).Extract()
	if err != nil {
		resource.Err = err
		return
	}
	resource.Result = securityGroupSingle(securityGroup)
}
Esempio n. 15
0
func (command *commandDelete) Execute(resource *handler.Resource) {
	keypairName := resource.Params.(*paramsDelete).keypair
	err := keypairs.Delete(command.Ctx.ServiceClient, keypairName).ExtractErr()
	if err != nil {
		resource.Err = err
		return
	}
	resource.Result = fmt.Sprintf("Successfully deleted keypair [%s]\n", keypairName)
}
Esempio n. 16
0
func (command *commandGetMetadata) Execute(resource *handler.Resource) {
	serverID := resource.Params.(*paramsGetMetadata).serverID
	metadata, err := servers.Metadata(command.Ctx.ServiceClient, serverID).Extract()
	if err != nil {
		resource.Err = err
		return
	}
	resource.Result = metadata
}
Esempio n. 17
0
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)
}
Esempio n. 18
0
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)
}
Esempio n. 19
0
File: get.go Progetto: flazz/rack
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)
}
Esempio n. 20
0
func (command *commandGet) Execute(resource *handler.Resource) {
	volumeID := resource.Params.(*paramsGet).volumeID
	volume, err := osVolumes.Get(command.Ctx.ServiceClient, volumeID).Extract()
	if err != nil {
		resource.Err = err
		return
	}
	resource.Result = volumeSingle(volume)
}
Esempio n. 21
0
File: create.go Progetto: flazz/rack
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)
}
Esempio n. 22
0
func (command *commandDelete) Execute(resource *handler.Resource) {
	volumeID := resource.Params.(*paramsDelete).volumeID
	err := osVolumes.Delete(command.Ctx.ServiceClient, volumeID).ExtractErr()
	if err != nil {
		resource.Err = err
		return
	}
	resource.Result = fmt.Sprintf("Deleting volume [%s]\n", volumeID)
}
Esempio n. 23
0
func (command *commandGet) Execute(resource *handler.Resource) {
	flavorID := resource.Params.(*paramsGet).flavor
	flavor, err := flavors.Get(command.Ctx.ServiceClient, flavorID).Extract()
	if err != nil {
		resource.Err = err
		return
	}
	resource.Result = structs.Map(flavor)
}
Esempio n. 24
0
File: get.go Progetto: hdansou/rack
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)
}
Esempio n. 25
0
func (command *commandGenerate) Execute(resource *handler.Resource) {
	opts := resource.Params.(*paramsGenerate).opts
	keypair, err := keypairs.Create(command.Ctx.ServiceClient, opts).Extract()
	if err != nil {
		resource.Err = err
		return
	}
	resource.Result = structs.Map(keypair)
}
Esempio n. 26
0
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)
}
Esempio n. 27
0
File: delete.go Progetto: flazz/rack
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)
}
Esempio n. 28
0
File: create.go Progetto: flazz/rack
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)
}
Esempio n. 29
0
File: get.go Progetto: hdansou/rack
func (command *commandGet) Execute(resource *handler.Resource) {
	snapshotID := resource.Params.(*paramsGet).snapshotID
	snapshot, err := osSnapshots.Get(command.Ctx.ServiceClient, snapshotID).Extract()
	if err != nil {
		resource.Err = err
		return
	}
	resource.Result = snapshotSingle(snapshot)
}
Esempio n. 30
0
File: resize.go Progetto: flazz/rack
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)
}