tags

package
v0.42.0 Latest Latest
Warning

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

Go to latest
Published: Aug 16, 2024 License: Apache-2.0 Imports: 8 Imported by: 125

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type AttachedObjects added in v0.21.0

type AttachedObjects struct {
	TagID     string         `json:"tag_id"`
	Tag       *Tag           `json:"tag,omitempty"`
	ObjectIDs []mo.Reference `json:"object_ids"`
}

AttachedObjects is the response type used by ListAttachedObjectsOnTags.

func (*AttachedObjects) UnmarshalJSON added in v0.21.0

func (t *AttachedObjects) UnmarshalJSON(b []byte) error

UnmarshalJSON implements json.Unmarshaler.

type AttachedTags added in v0.21.0

type AttachedTags struct {
	ObjectID mo.Reference `json:"object_id"`
	TagIDs   []string     `json:"tag_ids"`
	Tags     []Tag        `json:"tags,omitempty"`
}

AttachedTags is the response type used by ListAttachedTagsOnObjects.

func (*AttachedTags) UnmarshalJSON added in v0.21.0

func (t *AttachedTags) UnmarshalJSON(b []byte) error

UnmarshalJSON implements json.Unmarshaler.

type BatchError added in v0.24.0

type BatchError struct {
	Type    string `json:"id"`
	Message string `json:"default_message"`
}

BatchError is an error returned for a single item which failed in a batch operation

type BatchErrors added in v0.24.0

type BatchErrors []BatchError

BatchErrors contains all errors which occurred in a batch operation

func (BatchErrors) Error added in v0.24.0

func (b BatchErrors) Error() string

type Category

type Category struct {
	ID              string   `json:"id,omitempty"`
	Name            string   `json:"name,omitempty"`
	Description     string   `json:"description,omitempty"`
	Cardinality     string   `json:"cardinality,omitempty"`
	AssociableTypes []string `json:"associable_types,omitempty"`
	UsedBy          []string `json:"used_by,omitempty"`
}

Category provides methods to create, read, update, delete, and enumerate categories.

func (*Category) Patch

func (c *Category) Patch(src *Category)

Patch merges Category changes from the given src. AssociableTypes can only be appended to and cannot shrink.

type Manager

type Manager struct {
	*rest.Client
}

Manager extends rest.Client, adding tag related methods.

func NewManager

func NewManager(client *rest.Client) *Manager

NewManager creates a new Manager instance with the given client.

func (*Manager) AttachMultipleTagsToObject added in v0.24.0

func (c *Manager) AttachMultipleTagsToObject(ctx context.Context, tagIDs []string, ref mo.Reference) error

AttachMultipleTagsToObject attaches multiple tag IDs to a managed object. This operation is idempotent. If a tag is already attached to the object, then the individual operation is a no-op and no error will be thrown. This operation is not atomic. If the underlying call fails with one or more tags not successfully attached to the managed object reference it might leave the managed object reference in a partially tagged state and needs to be resolved by the caller. In this case BatchErrors is returned and can be used to analyse failure reasons on each failed tag.

Specified tagIDs must use URN-notation instead of display names or a generic error will be returned and no tagging operation will be performed. If the managed object reference does not exist a generic 403 Forbidden error will be returned.

This operation was added in vSphere API 6.5.

func (*Manager) AttachTag

func (c *Manager) AttachTag(ctx context.Context, tagID string, ref mo.Reference) error

AttachTag attaches a tag ID to a managed object.

func (*Manager) AttachTagToMultipleObjects added in v0.23.0

func (c *Manager) AttachTagToMultipleObjects(ctx context.Context, tagID string, refs []mo.Reference) error

AttachTagToMultipleObjects attaches a tag ID to multiple managed objects. This operation is idempotent, i.e. if a tag is already attached to the object, then the individual operation is a no-op and no error will be thrown.

This operation was added in vSphere API 6.5.

func (*Manager) CreateCategory

func (c *Manager) CreateCategory(ctx context.Context, category *Category) (string, error)

CreateCategory creates a new category and returns the category ID.

func (*Manager) CreateTag

func (c *Manager) CreateTag(ctx context.Context, tag *Tag) (string, error)

CreateTag creates a new tag with the given Name, Description and CategoryID.

Example
package main

import (
	"context"
	"fmt"

	"github.com/vmware/govmomi/simulator"
	"github.com/vmware/govmomi/vapi/rest"
	"github.com/vmware/govmomi/vapi/tags"
	"github.com/vmware/govmomi/vim25"

	_ "github.com/vmware/govmomi/vapi/simulator"
)

func main() {
	simulator.Run(func(ctx context.Context, vc *vim25.Client) error {
		c := rest.NewClient(vc)
		_ = c.Login(ctx, simulator.DefaultLogin)

		m := tags.NewManager(c)

		id, err := m.CreateCategory(ctx, &tags.Category{
			AssociableTypes: []string{"VirtualMachine"},
			Cardinality:     "SINGLE",
			Description:     "This is My Category",
			Name:            "my-category",
		})
		if err != nil {
			return err
		}

		id, err = m.CreateTag(ctx, &tags.Tag{
			CategoryID:  id,
			Description: "This is My Tag",
			Name:        "my-tag",
		})
		if err != nil {
			return err
		}

		tag, err := m.GetTag(ctx, id)
		if err != nil {
			return err
		}

		fmt.Println(tag.Name)
		return nil
	})
}
Output:

my-tag

func (*Manager) DeleteCategory

func (c *Manager) DeleteCategory(ctx context.Context, category *Category) error

DeleteCategory deletes a category.

func (*Manager) DeleteTag

func (c *Manager) DeleteTag(ctx context.Context, tag *Tag) error

DeleteTag deletes an existing tag.

func (*Manager) DetachMultipleTagsFromObject added in v0.24.0

func (c *Manager) DetachMultipleTagsFromObject(ctx context.Context, tagIDs []string, ref mo.Reference) error

DetachMultipleTagsFromObject detaches multiple tag IDs from a managed object. This operation is idempotent. If a tag is already detached from the object, then the individual operation is a no-op and no error will be thrown. This operation is not atomic. If the underlying call fails with one or more tags not successfully detached from the managed object reference it might leave the managed object reference in a partially tagged state and needs to be resolved by the caller. In this case BatchErrors is returned and can be used to analyse failure reasons on each failed tag.

Specified tagIDs must use URN-notation instead of display names or a generic error will be returned and no tagging operation will be performed. If the managed object reference does not exist a generic 403 Forbidden error will be returned.

This operation was added in vSphere API 6.5.

func (*Manager) DetachTag

func (c *Manager) DetachTag(ctx context.Context, tagID string, ref mo.Reference) error

DetachTag detaches a tag ID from a managed object. If the tag is already removed from the object, then this operation is a no-op and an error will not be thrown.

func (*Manager) GetAttachedObjectsOnTags added in v0.21.0

func (c *Manager) GetAttachedObjectsOnTags(ctx context.Context, tagID []string) ([]AttachedObjects, error)

GetAttachedObjectsOnTags combines ListAttachedObjectsOnTags and populates each Tag field.

func (*Manager) GetAttachedTags

func (c *Manager) GetAttachedTags(ctx context.Context, ref mo.Reference) ([]Tag, error)

GetAttachedTags fetches the array of tags attached to the given object.

func (*Manager) GetAttachedTagsOnObjects added in v0.21.0

func (c *Manager) GetAttachedTagsOnObjects(ctx context.Context, objectID []mo.Reference) ([]AttachedTags, error)

GetAttachedTagsOnObjects calls ListAttachedTagsOnObjects and populates each Tags field.

Example
package main

import (
	"context"
	"fmt"
	"log"

	"github.com/vmware/govmomi/property"
	"github.com/vmware/govmomi/simulator"
	"github.com/vmware/govmomi/vapi/rest"
	"github.com/vmware/govmomi/vapi/tags"
	"github.com/vmware/govmomi/view"
	"github.com/vmware/govmomi/vim25"
	"github.com/vmware/govmomi/vim25/mo"

	_ "github.com/vmware/govmomi/vapi/simulator"
)

func main() {
	simulator.Run(func(ctx context.Context, vc *vim25.Client) error {
		c := rest.NewClient(vc)
		_ = c.Login(ctx, simulator.DefaultLogin)

		m := tags.NewManager(c)

		id, err := m.CreateCategory(ctx, &tags.Category{Name: "my-category"})
		if err != nil {
			return err
		}

		id, err = m.CreateTag(ctx, &tags.Tag{CategoryID: id, Name: "my-tag"})
		if err != nil {
			return err
		}

		v, err := view.NewManager(vc).CreateContainerView(ctx, vc.ServiceContent.RootFolder, []string{"VirtualMachine"}, true)
		if err != nil {
			log.Fatal(err)
		}

		vms, err := v.Find(ctx, nil, property.Match{}) // List all VMs in the inventory
		if err != nil {
			return err
		}
		refs := make([]mo.Reference, len(vms)) // Convert list type
		for i := range vms {
			refs[i] = vms[i]
		}

		for i := 0; i < len(refs)/2; i++ { // AttachTag to half of the VMs
			if err = m.AttachTag(ctx, id, refs[i]); err != nil {
				return err
			}
		}

		attached, err := m.GetAttachedTagsOnObjects(ctx, refs) // Get AttachedTags for all VMs
		if err != nil {
			return err
		}

		n := 0
		for _, a := range attached { // Count tags attached to all VMs
			n += len(a.Tags)
		}

		fmt.Printf("%d of %d vms are tagged", n, len(vms))
		return nil
	})
}
Output:

2 of 4 vms are tagged

func (*Manager) GetCategories

func (c *Manager) GetCategories(ctx context.Context) ([]Category, error)

GetCategories fetches a list of category information in the system.

func (*Manager) GetCategory

func (c *Manager) GetCategory(ctx context.Context, id string) (*Category, error)

GetCategory fetches the category information for the given identifier. The id parameter can be a Category ID or Category Name.

func (*Manager) GetTag

func (c *Manager) GetTag(ctx context.Context, id string) (*Tag, error)

GetTag fetches the tag information for the given identifier. The id parameter can be a Tag ID or Tag Name.

func (*Manager) GetTagForCategory added in v0.20.0

func (c *Manager) GetTagForCategory(ctx context.Context, id, category string) (*Tag, error)

GetTagForCategory fetches the tag information for the given identifier in the given category.

func (*Manager) GetTags

func (c *Manager) GetTags(ctx context.Context) ([]Tag, error)

GetTags fetches an array of tag information in the system.

func (*Manager) GetTagsForCategory

func (c *Manager) GetTagsForCategory(ctx context.Context, id string) ([]Tag, error)

The id parameter can be a Category ID or Category Name.

func (*Manager) ListAttachedObjects

func (c *Manager) ListAttachedObjects(ctx context.Context, tagID string) ([]mo.Reference, error)

ListAttachedObjects fetches the array of attached objects for the given tag ID.

func (*Manager) ListAttachedObjectsOnTags added in v0.21.0

func (c *Manager) ListAttachedObjectsOnTags(ctx context.Context, tagID []string) ([]AttachedObjects, error)

ListAttachedObjectsOnTags fetches the array of attached objects for the given tag IDs.

func (*Manager) ListAttachedTags

func (c *Manager) ListAttachedTags(ctx context.Context, ref mo.Reference) ([]string, error)

ListAttachedTags fetches the array of tag IDs attached to the given object.

func (*Manager) ListAttachedTagsOnObjects added in v0.21.0

func (c *Manager) ListAttachedTagsOnObjects(ctx context.Context, objectID []mo.Reference) ([]AttachedTags, error)

ListAttachedTagsOnObjects fetches the array of attached tag IDs for the given object IDs.

func (*Manager) ListCategories

func (c *Manager) ListCategories(ctx context.Context) ([]string, error)

ListCategories returns all category IDs in the system.

func (*Manager) ListTags

func (c *Manager) ListTags(ctx context.Context) ([]string, error)

ListTags returns all tag IDs in the system.

func (*Manager) ListTagsForCategory

func (c *Manager) ListTagsForCategory(ctx context.Context, id string) ([]string, error)

The id parameter can be a Category ID or Category Name.

func (*Manager) UpdateCategory

func (c *Manager) UpdateCategory(ctx context.Context, category *Category) error

UpdateCategory updates one or more of the AssociableTypes, Cardinality, Description and Name fields.

func (*Manager) UpdateTag

func (c *Manager) UpdateTag(ctx context.Context, tag *Tag) error

UpdateTag can update one or both of the tag Description and Name fields.

type Tag

type Tag struct {
	ID          string   `json:"id,omitempty"`
	Description string   `json:"description,omitempty"`
	Name        string   `json:"name,omitempty"`
	CategoryID  string   `json:"category_id,omitempty"`
	UsedBy      []string `json:"used_by,omitempty"`
}

Tag provides methods to create, read, update, delete, and enumerate tags.

func (*Tag) Patch

func (t *Tag) Patch(src *Tag)

Patch merges updates from the given src.

Jump to

Keyboard shortcuts

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