From 4edefb2c080acf696449c42137791db42763876f Mon Sep 17 00:00:00 2001 From: Simon Fuhrer Date: Wed, 17 Jun 2020 12:30:28 +0200 Subject: [PATCH 1/2] update docs --- category.go | 15 +++++++-------- image.go | 14 +++++++------- project.go | 14 +++++++------- schema/helper.go | 32 ++++++++++++++++++++++++++++++++ subnet.go | 16 ++++++++-------- vm.go | 40 +++++++--------------------------------- 6 files changed, 68 insertions(+), 63 deletions(-) create mode 100644 schema/helper.go diff --git a/category.go b/category.go index c05bdf6..f9d3aac 100644 --- a/category.go +++ b/category.go @@ -15,13 +15,12 @@ const ( categoryNameListPath = categorySinglePath + "/list" ) -// CategoryClient is a client for the image API. +// CategoryClient is a client for the category API type CategoryClient struct { client *Client } -// Get retrieves an image by its ID if the input can be parsed as an integer, otherwise it -// retrieves an image by its name. If the image does not exist, nil is returned. +// Get retrieves an catagory by its name. func (c *CategoryClient) Get(ctx context.Context, idOrName string) (*schema.CategoryKeyStatus, error) { if utils.IsValidUUID(idOrName) { return c.GetByUUID(ctx, idOrName) @@ -29,14 +28,14 @@ func (c *CategoryClient) Get(ctx context.Context, idOrName string) (*schema.Cate return c.GetByName(ctx, idOrName) } -// GetByUUID retrieves an image by its UUID. If the image does not exist, nil is returned. +// GetByUUID retrieves an category by its UUID. func (c *CategoryClient) GetByUUID(ctx context.Context, uuid string) (*schema.CategoryKeyStatus, error) { response := new(schema.CategoryKeyStatus) err := c.client.requestHelper(ctx, fmt.Sprintf(categorySinglePath, uuid), "GET", nil, response) return response, err } -// GetByName retrieves an image by its name. If the image does not exist, nil is returned. +// GetByName retrieves an category by its name. func (c *CategoryClient) GetByName(ctx context.Context, name string) (*schema.CategoryKeyStatus, error) { categories, err := c.List(ctx, &schema.DSMetadata{Filter: fmt.Sprintf("name==%s", name)}) if err != nil { @@ -48,14 +47,14 @@ func (c *CategoryClient) GetByName(ctx context.Context, name string) (*schema.Ca return categories.Entities[0], err } -// List returns a list of images for a specific page. +// List returns a list of all CategoryKeyStatus func (c *CategoryClient) List(ctx context.Context, opts *schema.DSMetadata) (*schema.CategoryKeyList, error) { response := new(schema.CategoryKeyList) err := c.client.listHelper(ctx, categoryListPath, opts, response) return response, err } -// ListValues returns a list of images for a specific page. +// ListValues returns a list of schema.CategoryValueList func (c *CategoryClient) ListValues(ctx context.Context, name string) (*schema.CategoryValueList, error) { response := new(schema.CategoryValueList) err := c.client.requestHelper(ctx, fmt.Sprintf(categoryNameListPath, name), "POST", &schema.DSMetadata{}, response) @@ -69,7 +68,7 @@ func (c *CategoryClient) Create(ctx context.Context, createRequest *schema.Categ return response, err } -// All returns all CategoryKeyStatus. +// All returns all CategoryKeyStatus func (c *CategoryClient) All(ctx context.Context) (*schema.CategoryKeyList, error) { return c.List(ctx, &schema.DSMetadata{Length: utils.Int64Ptr(itemsPerPage), Offset: utils.Int64Ptr(0)}) } diff --git a/image.go b/image.go index edd6e9e..0f311f7 100644 --- a/image.go +++ b/image.go @@ -22,8 +22,8 @@ type ImageClient struct { client *Client } -// Get retrieves an image by its ID if the input can be parsed as an integer, otherwise it -// retrieves an image by its name. If the image does not exist, nil is returned. +// Get retrieves an image by its ID if the input can be parsed as an uuid, otherwise it +// retrieves an image by its name func (c *ImageClient) Get(ctx context.Context, idOrName string) (*schema.ImageIntent, error) { if utils.IsValidUUID(idOrName) { return c.GetByUUID(ctx, idOrName) @@ -31,14 +31,14 @@ func (c *ImageClient) Get(ctx context.Context, idOrName string) (*schema.ImageIn return c.GetByName(ctx, idOrName) } -// GetByUUID retrieves an image by its UUID. If the image does not exist, nil is returned. +// GetByUUID retrieves an image by its UUID func (c *ImageClient) GetByUUID(ctx context.Context, uuid string) (*schema.ImageIntent, error) { response := new(schema.ImageIntent) err := c.client.requestHelper(ctx, fmt.Sprintf(imageSinglePath, uuid), http.MethodGet, nil, response) return response, err } -// GetByName retrieves an image by its name. If the image does not exist, nil is returned. +// GetByName retrieves an image by its name func (c *ImageClient) GetByName(ctx context.Context, name string) (*schema.ImageIntent, error) { images, err := c.List(ctx, &schema.DSMetadata{Filter: fmt.Sprintf("name==%s", name)}) if err != nil { @@ -50,19 +50,19 @@ func (c *ImageClient) GetByName(ctx context.Context, name string) (*schema.Image return images.Entities[0], err } -// List returns a list of images for a specific page. +// List returns a list of images func (c *ImageClient) List(ctx context.Context, opts *schema.DSMetadata) (*schema.ImageListIntent, error) { response := new(schema.ImageListIntent) err := c.client.listHelper(ctx, imageListPath, opts, response) return response, err } -// All returns all images. +// All returns all images func (c *ImageClient) All(ctx context.Context) (*schema.ImageListIntent, error) { return c.List(ctx, &schema.DSMetadata{Length: utils.Int64Ptr(itemsPerPage), Offset: utils.Int64Ptr(0)}) } -// Upload ... +// Upload a qcow2 func (c *ImageClient) Upload(ctx context.Context, uuid string, fileContents []byte) (*schema.ImageIntent, error) { file := &schema.File{ diff --git a/project.go b/project.go index af96268..3f74269 100644 --- a/project.go +++ b/project.go @@ -20,8 +20,8 @@ type ProjectClient struct { client *Client } -// Get retrieves an project by its ID if the input can be parsed as an integer, otherwise it -// retrieves an image by its name. If the image does not exist, nil is returned. +// Get retrieves an project by its UUID if the input can be parsed as an uuid, otherwise it +// retrieves a project by its name func (c *ProjectClient) Get(ctx context.Context, idOrName string) (*schema.ProjectIntent, error) { if utils.IsValidUUID(idOrName) { return c.GetByUUID(ctx, idOrName) @@ -29,14 +29,14 @@ func (c *ProjectClient) Get(ctx context.Context, idOrName string) (*schema.Proje return c.GetByName(ctx, idOrName) } -// GetByUUID retrieves an image by its UUID. If the image does not exist, nil is returned. +// GetByUUID retrieves an project by its UUID func (c *ProjectClient) GetByUUID(ctx context.Context, uuid string) (*schema.ProjectIntent, error) { response := new(schema.ProjectIntent) err := c.client.requestHelper(ctx, fmt.Sprintf(projectSinglePath, uuid), http.MethodGet, nil, response) return response, err } -// GetByName retrieves an project by its name. If the project does not exist, nil is returned. +// GetByName retrieves an project by its name func (c *ProjectClient) GetByName(ctx context.Context, name string) (*schema.ProjectIntent, error) { list, err := c.List(ctx, &schema.DSMetadata{Filter: fmt.Sprintf("name==%s", name)}) if err != nil { @@ -48,14 +48,14 @@ func (c *ProjectClient) GetByName(ctx context.Context, name string) (*schema.Pro return list.Entities[0], err } -// List returns a list of projects for a specific page. +// List returns a list of projects func (c *ProjectClient) List(ctx context.Context, opts *schema.DSMetadata) (*schema.ProjectListIntent, error) { response := new(schema.ProjectListIntent) err := c.client.listHelper(ctx, projectListPath, opts, response) return response, err } -// All returns all images. +// All returns all projects func (c *ProjectClient) All(ctx context.Context) (*schema.ProjectListIntent, error) { return c.List(ctx, &schema.DSMetadata{Length: utils.Int64Ptr(itemsPerPage), Offset: utils.Int64Ptr(0)}) } @@ -75,7 +75,7 @@ func (c *ProjectClient) Update(ctx context.Context, project *schema.ProjectInten return response, err } -// Delete deletes a Project +// Delete deletes a project func (c *ProjectClient) Delete(ctx context.Context, uuid string) error { return c.client.requestHelper(ctx, fmt.Sprintf(projectSinglePath, uuid), http.MethodDelete, nil, nil) } diff --git a/schema/helper.go b/schema/helper.go new file mode 100644 index 0000000..cf21930 --- /dev/null +++ b/schema/helper.go @@ -0,0 +1,32 @@ +package schema + +import ( + "encoding/base64" + "encoding/json" + + "github.com/google/uuid" +) + +type VMRevertRequest struct { + VMRecoveryPointUUID *string `json:"vm_recovery_point_uuid"` +} + +type MetaData struct { + SSHAuthorizedKeyMap map[string]string `json:"public_keys,omitempty"` + Hostname string `json:"hostname"` + UUID string `json:"uuid"` + AvailabilityZone string `json:"availability_zone,omitempty"` + Project string `json:"project_id,omitempty"` +} + +func (m *MetaData) ToBase64() (string, error) { + if m.UUID == "" { + uuid, _ := uuid.NewRandom() + m.UUID = uuid.String() + } + j, err := json.Marshal(m) + if err != nil { + return "", err + } + return base64.StdEncoding.EncodeToString(j), nil +} diff --git a/subnet.go b/subnet.go index 716388c..ce36b7b 100644 --- a/subnet.go +++ b/subnet.go @@ -20,8 +20,8 @@ type SubnetClient struct { client *Client } -// Get retrieves an image by its ID if the input can be parsed as an integer, otherwise it -// retrieves an image by its name. If the image does not exist, nil is returned. +// Get retrieves an subnet by its UUID if the input can be parsed as an uuid, otherwise it +// retrieves an subnet by its name func (c *SubnetClient) Get(ctx context.Context, idOrName string) (*schema.SubnetIntent, error) { if utils.IsValidUUID(idOrName) { return c.GetByUUID(ctx, idOrName) @@ -29,14 +29,14 @@ func (c *SubnetClient) Get(ctx context.Context, idOrName string) (*schema.Subnet return c.GetByName(ctx, idOrName) } -// GetByUUID retrieves an image by its UUID. If the image does not exist, nil is returned. +// GetByUUID retrieves a subnet by its UUID func (c *SubnetClient) GetByUUID(ctx context.Context, uuid string) (*schema.SubnetIntent, error) { response := new(schema.SubnetIntent) err := c.client.requestHelper(ctx, fmt.Sprintf(subnetSinglePath, uuid), http.MethodGet, nil, response) return response, err } -// GetByName retrieves an subnet by its name. If the image does not exist, nil is returned. +// GetByName retrieves an subnet by its name func (c *SubnetClient) GetByName(ctx context.Context, name string) (*schema.SubnetIntent, error) { list, err := c.List(ctx, &schema.DSMetadata{Filter: fmt.Sprintf("name==%s", name)}) if err != nil { @@ -55,12 +55,12 @@ func (c *SubnetClient) List(ctx context.Context, opts *schema.DSMetadata) (*sche return response, err } -// All returns all images. +// All returns all subnets func (c *SubnetClient) All(ctx context.Context) (*schema.SubnetListIntent, error) { return c.List(ctx, &schema.DSMetadata{Length: utils.Int64Ptr(itemsPerPage), Offset: utils.Int64Ptr(0)}) } -// Update a project +// Update a subnet func (c *SubnetClient) Update(ctx context.Context, updateRequest *schema.SubnetIntent) (*schema.SubnetIntent, error) { updateRequest.Status = nil response := new(schema.SubnetIntent) @@ -68,14 +68,14 @@ func (c *SubnetClient) Update(ctx context.Context, updateRequest *schema.SubnetI return response, err } -// Create creates a subnet +// Create a subnet func (c *SubnetClient) Create(ctx context.Context, createRequest *schema.SubnetIntent) (*schema.SubnetIntent, error) { response := new(schema.SubnetIntent) err := c.client.requestHelper(ctx, subnetBasePath, http.MethodPost, createRequest, response) return response, err } -// Delete deletes a Subnet +// Delete a subnet func (c *SubnetClient) Delete(ctx context.Context, uuid string) error { return c.client.requestHelper(ctx, fmt.Sprintf(subnetSinglePath, uuid), http.MethodDelete, nil, nil) } diff --git a/vm.go b/vm.go index 248a90d..2f95659 100644 --- a/vm.go +++ b/vm.go @@ -3,13 +3,10 @@ package nutanix import ( "bytes" "context" - "encoding/base64" "encoding/json" "fmt" "net/http" - "github.com/google/uuid" - "github.com/tecbiz-ch/nutanix-go-sdk/internal/utils" "github.com/tecbiz-ch/nutanix-go-sdk/schema" v2 "github.com/tecbiz-ch/nutanix-go-sdk/schema/v2" @@ -25,37 +22,13 @@ const ( vmSnapshotPath = vmSinglePath + "/snapshot" ) -type MetaData struct { - SSHAuthorizedKeyMap map[string]string `json:"public_keys,omitempty"` - Hostname string `json:"hostname"` - UUID string `json:"uuid"` - AvailabilityZone string `json:"availability_zone,omitempty"` - Project string `json:"project_id,omitempty"` -} - -func (m *MetaData) ToBase64() (string, error) { - if m.UUID == "" { - uuid, _ := uuid.NewRandom() - m.UUID = uuid.String() - } - j, err := json.Marshal(m) - if err != nil { - return "", err - } - return base64.StdEncoding.EncodeToString(j), nil -} - -// VMClient is a client for the vm API. +// VMClient is a client for the VM API. type VMClient struct { client *Client } -// VMRevertRequest ... -type VMRevertRequest struct { - VMRecoveryPointUUID *string `json:"vm_recovery_point_uuid"` -} - -// Get ... +// Get retrieves an vm by its UUID if the input can be parsed as an uuid, otherwise it +// retrieves an vm by its name func (c *VMClient) Get(ctx context.Context, idOrName string) (*schema.VMIntent, error) { if utils.IsValidUUID(idOrName) { return c.GetByUUID(ctx, idOrName) @@ -63,14 +36,14 @@ func (c *VMClient) Get(ctx context.Context, idOrName string) (*schema.VMIntent, return c.GetByName(ctx, idOrName) } -// GetByUUID retrieves an vm by its UUID. If the vm does not exist, nil is returned. +// GetByUUID retrieves an vm by its UUID func (c *VMClient) GetByUUID(ctx context.Context, uuid string) (*schema.VMIntent, error) { response := new(schema.VMIntent) err := c.client.requestHelper(ctx, fmt.Sprintf(vmSinglePath, uuid), http.MethodGet, nil, response) return response, err } -// GetByName retrieves an vm by its name. If the vm does not exist, nil is returned. +// GetByName retrieves an vm by its name func (c *VMClient) GetByName(ctx context.Context, name string) (*schema.VMIntent, error) { vms, err := c.List(ctx, &schema.DSMetadata{Filter: fmt.Sprintf("vm_name==%s", name)}) if err != nil { @@ -115,7 +88,7 @@ func (c *VMClient) Delete(ctx context.Context, uuid string) error { } // RevertToRecoveryPoint ... -func (c *VMClient) RevertToRecoveryPoint(ctx context.Context, vm *schema.VMIntent, vmRevertRequest *VMRevertRequest) (*v2.Task, error) { +func (c *VMClient) RevertToRecoveryPoint(ctx context.Context, vm *schema.VMIntent, vmRevertRequest *schema.VMRevertRequest) (*v2.Task, error) { reqBodyData, err := json.Marshal(&vmRevertRequest) if err != nil { return nil, err @@ -199,6 +172,7 @@ func (c *VMClient) SetPowerState(ctx context.Context, powerState v2.PowerState, return response, nil } +// Clone ... func (c *VMClient) Clone(ctx context.Context, sourcevm *schema.VMIntent) (*v2.Task, error) { // TODO: From 06e1264bdcce77d427a3b49c70156934d0335271 Mon Sep 17 00:00:00 2001 From: Simon Fuhrer Date: Wed, 17 Jun 2020 12:43:32 +0200 Subject: [PATCH 2/2] add example --- README.md | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/README.md b/README.md index 150fe55..57a9e6e 100644 --- a/README.md +++ b/README.md @@ -22,6 +22,25 @@ import ( ) func main() { + + configCreds := nutanix.Credentials{ + Username: "admin", + Password: "password", + } + + opts := []nutanix.ClientOption{ + nutanix.WithCredentials(&configCreds), + nutanix.WithEndpoint("https://PC"), + nutanix.WithInsecure(), // Allow insecure + } + + client := nutanix.NewClient(opts...) + + ctx := context.Background() + mycluster, err = client.Cluster.Get(ctx, "mycluster") + + list, err = client.VM.All(ctx) + } ```