resources

package
v0.0.0-...-80377ec Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 10, 2023 License: Apache-2.0 Imports: 6 Imported by: 3

Documentation

Overview

Package resources provides the ability to retrieve resources through the Gnocchi API.

Example of Listing resources

resourceType: "instance",
listOpts := resources.ListOpts{
	Details: True,
}

allPages, err := resources.List(gnocchiClient, listOpts, resourceType).AllPages()
if err != nil {
	panic(err)
}

allResources, err := resources.ExtractResources(allPages)
if err != nil {
	panic(err)
}

for _, resource := range allResources {
	fmt.Printf("%+v\n", resource)
}

Example of Getting a resource

resourceID = "23d5d3f7-9dfa-4f73-b72b-8b0b0063ec55"
resourceType = "generic"
resource, err := resources.Get(gnocchiClient, resourceType, resourceID).Extract()
if err != nil {
	panic(err)
}

Example of Creating a resource without a metric

createOpts := resources.CreateOpts{
	ID: "23d5d3f7-9dfa-4f73-b72b-8b0b0063ec55",
}
resourceType = "generic"
resource, err := resources.Create(gnocchiClient, resourceType, createOpts).Extract()
if err != nil {
	panic(err)
}

Example of Creating a resource with links to some existing metrics with a starting timestamp of the resource

startedAt := time.Date(2018, 1, 4, 10, 0, 0, 0, time.UTC)
createOpts := resources.CreateOpts{
	ID: "23d5d3f7-9dfa-4f73-b72b-8b0b0063ec55",
	ProjectID: "4154f088-8333-4e04-94c4-1155c33c0fc9",
	StartedAt: &startedAt,
	Metrics: map[string]interface{}{
		"disk.read.bytes.rate": "ed1bb76f-6ccc-4ad2-994c-dbb19ddccbae",
		"disk.write.bytes.rate": "0a2da84d-4753-43f5-a65f-0f8d44d2766c",
	},
}
resourceType = "compute_instance_disk"
resource, err := resources.Create(gnocchiClient, resourceType, createOpts).Extract()
if err != nil {
	panic(err)
}

Example of Creating a resource and a metric a the same time

createOpts := resources.CreateOpts{
	ID: "23d5d3f7-9dfa-4f73-b72b-8b0b0063ec55",
	ProjectID: "4154f088-8333-4e04-94c4-1155c33c0fc9",
	UserID: "bd5874d6-6662-4b24-a9f01c128871e4ac",
	Metrics: map[string]interface{}{
		"cpu.delta": map[string]string{
			"archive_policy_name": "medium",
		},
	},
}
resourceType = "compute_instance"
resource, err := resources.Create(gnocchiClient, resourceType, createOpts).Extract()
if err != nil {
	panic(err)
}

Example of Updating a resource

updateOpts := resources.UpdateOpts{
	ProjectID: "4154f08883334e0494c41155c33c0fc9",
}
resourceType = "generic"
resourceID = "23d5d3f7-9dfa-4f73-b72b-8b0b0063ec55"
resource, err := resources.Update(gnocchiClient, resourceType, resourceID, updateOpts).Extract()
if err != nil {
	panic(err)
}

Example of Updating a resource and associating an existing metric to it

endedAt := time.Date(2018, 1, 16, 12, 0, 0, 0, time.UTC)
metrics := map[string]interface{}{
		"disk.write.bytes.rate": "0a2da84d-4753-43f5-a65f-0f8d44d2766c",
}
updateOpts := resources.UpdateOpts{
	EndedAt: &endedAt,
	Metrics: &metrics,
}
resourceType = "compute_instance_disk"
resourceID = "23d5d3f7-9dfa-4f73-b72b-8b0b0063ec55"
resource, err := resources.Update(gnocchiClient, resourceType, resourceID, updateOpts).Extract()
if err != nil {
	panic(err)
}

Example of Updating a resource and creating an associated metric at the same time

metrics := map[string]interface{}{
	"cpu.delta": map[string]string{
		"archive_policy_name": "medium",
	},
}
updateOpts := resources.UpdateOpts{
	Metrics: &metrics,
}
resourceType = "compute_instance"
resourceID = "23d5d3f7-9dfa-4f73-b72b-8b0b0063ec55"
resource, err := resources.Update(gnocchiClient, resourceType, resourceID, updateOpts).Extract()
if err != nil {
	panic(err)
}

Example of Deleting a Gnocchi resource

resourceID := "23d5d3f7-9dfa-4f73-b72b-8b0b0063ec55"
err := resources.Delete(gnocchiClient, resourceType, resourceID).ExtractErr()
if err != nil {
	panic(err)
}

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func List

func List(c *gophercloud.ServiceClient, opts ListOptsBuilder, resourceType string) pagination.Pager

List returns a Pager which allows you to iterate over a collection of resources. It accepts a ListOpts struct, which allows you to limit and sort the returned collection for a greater efficiency.

Types

type CreateOpts

type CreateOpts struct {
	// ID uniquely identifies the Gnocchi resource.
	ID string `json:"id" required:"true"`

	// Metrics field can be used to link existing metrics in the resource
	// or to create metrics with the resource at the same time to save
	// some requests.
	Metrics map[string]interface{} `json:"metrics,omitempty"`

	// ProjectID is the Identity project of the resource.
	ProjectID string `json:"project_id,omitempty"`

	// UserID is the Identity user of the resource.
	UserID string `json:"user_id,omitempty"`

	// StartedAt is a resource creation timestamp.
	StartedAt *time.Time `json:"-"`

	// EndedAt is a timestamp of when the resource has ended.
	EndedAt *time.Time `json:"-"`

	// ExtraAttributes is a collection of keys and values that can be found in resources
	// of different resource types.
	ExtraAttributes map[string]interface{} `json:"-"`
}

CreateOpts specifies parameters of a new Gnocchi resource.

func (CreateOpts) ToResourceCreateMap

func (opts CreateOpts) ToResourceCreateMap() (map[string]interface{}, error)

ToResourceCreateMap constructs a request body from CreateOpts.

type CreateOptsBuilder

type CreateOptsBuilder interface {
	ToResourceCreateMap() (map[string]interface{}, error)
}

CreateOptsBuilder allows to add additional parameters to the Create request.

type CreateResult

type CreateResult struct {
	// contains filtered or unexported fields
}

CreateResult represents the result of a create operation. Call its Extract method to interpret it as a Gnocchi resource.

func Create

func Create(client *gophercloud.ServiceClient, resourceType string, opts CreateOptsBuilder) (r CreateResult)

Create requests the creation of a new Gnocchi resource on the server.

func (CreateResult) Extract

func (r CreateResult) Extract() (*Resource, error)

Extract is a function that accepts a result and extracts a Gnocchi resource.

type DeleteResult

type DeleteResult struct {
	gophercloud.ErrResult
}

DeleteResult represents the result of a delete operation. Call its ExtractErr method to determine if the request succeeded or failed.

func Delete

func Delete(c *gophercloud.ServiceClient, resourceType, resourceID string) (r DeleteResult)

Delete accepts a unique ID and deletes the Gnocchi resource associated with it.

type GetResult

type GetResult struct {
	// contains filtered or unexported fields
}

GetResult represents the result of a get operation. Call its Extract method to interpret it as a Gnocchi resource.

func Get

func Get(c *gophercloud.ServiceClient, resourceType string, resourceID string) (r GetResult)

Get retrieves a specific Gnocchi resource based on its type and ID.

func (GetResult) Extract

func (r GetResult) Extract() (*Resource, error)

Extract is a function that accepts a result and extracts a Gnocchi resource.

type ListOpts

type ListOpts struct {
	// Details allows to list resources with all attributes.
	Details bool `q:"details"`

	// Limit allows to limits count of resources in the response.
	Limit int `q:"limit"`

	// Marker is used for pagination.
	Marker string `q:"marker"`

	// SortKey allows to sort resources in the response by key.
	SortKey string `q:"sort_key"`

	// SortDir allows to set the direction of sorting.
	// Can be `asc` or `desc`.
	SortDir string `q:"sort_dir"`
}

ListOpts allows the limiting and sorting of paginated collections through the Gnocchi API.

func (ListOpts) ToResourceListQuery

func (opts ListOpts) ToResourceListQuery() (string, error)

ToResourceListQuery formats a ListOpts into a query string.

type ListOptsBuilder

type ListOptsBuilder interface {
	ToResourceListQuery() (string, error)
}

ListOptsBuilder allows extensions to add additional parameters to the List request.

type Resource

type Resource struct {
	// CreatedByProjectID contains the id of the Identity project that
	// was used for a resource creation.
	CreatedByProjectID string `json:"created_by_project_id"`

	// CreatedByUserID contains the id of the Identity user
	// that created the Gnocchi resource.
	CreatedByUserID string `json:"created_by_user_id"`

	// Creator shows who created the resource.
	// Usually it contains concatenated string with values from
	// "created_by_user_id" and "created_by_project_id" fields.
	Creator string `json:"creator"`

	// ID uniquely identifies the Gnocchi resource.
	ID string `json:"id"`

	// Metrics are entities that store aggregates.
	Metrics map[string]string `json:"metrics"`

	// OriginalResourceID is the orginal resource id. It can be different from the
	// regular ID field.
	OriginalResourceID string `json:"original_resource_id"`

	// ProjectID is the Identity project of the resource.
	ProjectID string `json:"project_id"`

	// RevisionStart is a staring timestamp of the current resource revision.
	RevisionStart time.Time `json:"-"`

	// RevisionEnd is an ending timestamp of the last resource revision.
	RevisionEnd time.Time `json:"-"`

	// StartedAt is a resource creation timestamp.
	StartedAt time.Time `json:"-"`

	// EndedAt is a timestamp of when the resource has ended.
	EndedAt time.Time `json:"-"`

	// Type is a type of the resource.
	Type string `json:"type"`

	// UserID is the Identity user of the resource.
	UserID string `json:"user_id"`

	// ExtraAttributes is a collection of keys and values that can be found in resources
	// of different resource types.
	ExtraAttributes map[string]interface{} `json:"-"`
}

Resource is an entity representing anything in your infrastructure that you will associate metric(s) with. It is identified by a unique ID and can contain attributes.

func ExtractResources

func ExtractResources(r pagination.Page) ([]Resource, error)

ExtractResources interprets the results of a single page from a List() call, producing a slice of Resource structs.

func (*Resource) UnmarshalJSON

func (r *Resource) UnmarshalJSON(b []byte) error

UnmarshalJSON helps to unmarshal Resource fields into needed values.

type ResourcePage

type ResourcePage struct {
	pagination.MarkerPageBase
}

ResourcePage abstracts the raw results of making a List() request against the Gnocchi API.

As Gnocchi API may freely alter the response bodies of structures returned to the client, you may only safely access the data provided through the ExtractResources call.

func (ResourcePage) IsEmpty

func (r ResourcePage) IsEmpty() (bool, error)

IsEmpty checks whether a ResourcePage struct is empty.

func (ResourcePage) LastMarker

func (r ResourcePage) LastMarker() (string, error)

LastMarker returns the last resource ID in a ListResult.

type UpdateOpts

type UpdateOpts struct {
	// Metrics field can be used to link existing metrics in the resource
	// or to create metrics and update the resource at the same time to save
	// some requests.
	Metrics *map[string]interface{} `json:"metrics,omitempty"`

	// ProjectID is the Identity project of the resource.
	ProjectID string `json:"project_id,omitempty"`

	// UserID is the Identity user of the resource.
	UserID string `json:"user_id,omitempty"`

	// StartedAt is a resource creation timestamp.
	StartedAt *time.Time `json:"-"`

	// EndedAt is a timestamp of when the resource has ended.
	EndedAt *time.Time `json:"-"`

	// ExtraAttributes is a collection of keys and values that can be found in resources
	// of different resource types.
	ExtraAttributes map[string]interface{} `json:"-"`
}

UpdateOpts represents options used to update a network.

func (UpdateOpts) ToResourceUpdateMap

func (opts UpdateOpts) ToResourceUpdateMap() (map[string]interface{}, error)

ToResourceUpdateMap builds a request body from UpdateOpts.

type UpdateOptsBuilder

type UpdateOptsBuilder interface {
	ToResourceUpdateMap() (map[string]interface{}, error)
}

UpdateOptsBuilder allows extensions to add additional parameters to the Update request.

type UpdateResult

type UpdateResult struct {
	// contains filtered or unexported fields
}

UpdateResult represents the result of an update operation. Call its Extract method to interpret it as a Gnocchi resource.

func Update

func Update(c *gophercloud.ServiceClient, resourceType, resourceID string, opts UpdateOptsBuilder) (r UpdateResult)

Update accepts a UpdateOpts struct and updates an existing Gnocchi resource using the values provided.

func (UpdateResult) Extract

func (r UpdateResult) Extract() (*Resource, error)

Extract is a function that accepts a result and extracts a Gnocchi resource.

Directories

Path Synopsis
resources unit tests
resources unit tests

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL