Skip to content

Commit

Permalink
Merge pull request #4 from tecbiz-ch/dev
Browse files Browse the repository at this point in the history
Add Example
  • Loading branch information
simonfuhrer authored Jun 17, 2020
2 parents 2ecc3fc + 06e1264 commit c453394
Show file tree
Hide file tree
Showing 7 changed files with 87 additions and 63 deletions.
19 changes: 19 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

}
```

Expand Down
15 changes: 7 additions & 8 deletions category.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,28 +15,27 @@ 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)
}
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 {
Expand All @@ -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)
Expand All @@ -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)})
}
Expand Down
14 changes: 7 additions & 7 deletions image.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,23 +22,23 @@ 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)
}
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 {
Expand All @@ -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{
Expand Down
14 changes: 7 additions & 7 deletions project.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,23 +20,23 @@ 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)
}
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 {
Expand All @@ -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)})
}
Expand All @@ -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)
}
32 changes: 32 additions & 0 deletions schema/helper.go
Original file line number Diff line number Diff line change
@@ -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
}
16 changes: 8 additions & 8 deletions subnet.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,23 +20,23 @@ 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)
}
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 {
Expand All @@ -55,27 +55,27 @@ 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)
err := c.client.requestHelper(ctx, fmt.Sprintf(subnetSinglePath, updateRequest.Metadata.UUID), http.MethodPut, updateRequest, response)
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)
}
40 changes: 7 additions & 33 deletions vm.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -25,52 +22,28 @@ 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)
}
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 {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand Down

0 comments on commit c453394

Please sign in to comment.