Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

project quota #499

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
49 changes: 49 additions & 0 deletions client/calm/calm.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package calm

import (
"fmt"
"strings"

"github.com/terraform-providers/terraform-provider-nutanix/client"
)

const (
libraryVersion = "v3.0"
absolutePath = "api/calm/" + libraryVersion
userAgent = "nutanix/" + libraryVersion
clientName = "calm"
)

// Client manages the Calm API
type Client struct {
client *client.Client
V3 Service
}

// NewV3Client return a client to operate V3 resources
func NewV3Client(credentials client.Credentials) (*Client, error) {
var baseClient *client.Client

// check if all required fields are present. Else create an empty client
if credentials.Username != "" && credentials.Password != "" && credentials.Endpoint != "" {
c, err := client.NewClient(&credentials, userAgent, absolutePath, false)
if err != nil {
return nil, err
}
baseClient = c
} else {
errorMsg := fmt.Sprintf("Prism Central (PC) Client is missing. "+
"Please provide required details - %s in provider configuration.", strings.Join(credentials.RequiredFields[clientName], ", "))

baseClient = &client.Client{UserAgent: userAgent, ErrorMsg: errorMsg}
}

f := &Client{
client: baseClient,
V3: Operations{
client: baseClient,
},
}

return f, nil
}
54 changes: 54 additions & 0 deletions client/calm/calm_service.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package calm

import (
"context"
"fmt"
"net/http"

"github.com/terraform-providers/terraform-provider-nutanix/client"
)

// Operations implements Service interface
type Operations struct {
client *client.Client
}

type Service interface {
CreateProjectQuota(ctx context.Context, request *ProjectQuotaIntentInput) (*ProjectQuotaIntentResponse, error)
UpdateProjectQuota(ctx context.Context, quotaID string, request *ProjectQuotaIntentInput) (*ProjectQuotaIntentResponse, error)
EnableProjectQuota(ctx context.Context, request *EnableProjectQuotaInput) (*ProjectQuotaIntentResponse, error)
}

func (op Operations) CreateProjectQuota(ctx context.Context, request *ProjectQuotaIntentInput) (*ProjectQuotaIntentResponse, error) {
req, err := op.client.NewRequest(ctx, http.MethodPost, "/quotas", request)
if err != nil {
return nil, err
}

projectResponse := new(ProjectQuotaIntentResponse)

return projectResponse, op.client.Do(ctx, req, projectResponse)
}

func (op Operations) UpdateProjectQuota(ctx context.Context, quotaID string, request *ProjectQuotaIntentInput) (*ProjectQuotaIntentResponse, error) {
path := fmt.Sprintf("/quotas/%s", quotaID)
req, err := op.client.NewRequest(ctx, http.MethodPut, path, request)
if err != nil {
return nil, err
}

projectResponse := new(ProjectQuotaIntentResponse)

return projectResponse, op.client.Do(ctx, req, projectResponse)
}

func (op Operations) EnableProjectQuota(ctx context.Context, request *EnableProjectQuotaInput) (*ProjectQuotaIntentResponse, error) {
req, err := op.client.NewRequest(ctx, http.MethodPut, "/quotas/update/state", request)
if err != nil {
return nil, err
}

quotaEnableResponse := new(ProjectQuotaIntentResponse)

return quotaEnableResponse, op.client.Do(ctx, req, quotaEnableResponse)
}
96 changes: 96 additions & 0 deletions client/calm/calm_structs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
package calm

import "time"

// Reference ...
type Reference struct {
Kind *string `json:"kind" mapstructure:"kind"`
Name *string `json:"name,omitempty" mapstructure:"name,omitempty"`
UUID *string `json:"uuid" mapstructure:"uuid"`
}

// Metadata Metadata The kind metadata
type Metadata struct {
LastUpdateTime *time.Time `json:"last_update_time,omitempty" mapstructure:"last_update_time,omitempty"` //
Kind *string `json:"kind" mapstructure:"kind"` //
UUID *string `json:"uuid,omitempty" mapstructure:"uuid,omitempty"` //
ProjectReference *Reference `json:"project_reference,omitempty" mapstructure:"project_reference,omitempty"` // project reference
CreationTime *time.Time `json:"creation_time,omitempty" mapstructure:"creation_time,omitempty"`
SpecVersion *int64 `json:"spec_version,omitempty" mapstructure:"spec_version,omitempty"`
SpecHash *string `json:"spec_hash,omitempty" mapstructure:"spec_hash,omitempty"`
OwnerReference *Reference `json:"owner_reference,omitempty" mapstructure:"owner_reference,omitempty"`
Categories map[string]string `json:"categories,omitempty" mapstructure:"categories,omitempty"`
Name *string `json:"name,omitempty" mapstructure:"name,omitempty"`

// Applied on Prism Central only. Indicate whether force to translate the spec of the fanout request to fit the target cluster API schema.
ShouldForceTranslate *bool `json:"should_force_translate,omitempty" mapstructure:"should_force_translate,omitempty"`

//TODO: add if necessary
//CategoriesMapping map[string][]string `json:"categories_mapping,omitempty" mapstructure:"categories_mapping,omitempty"`
//EntityVersion *string `json:"entity_version,omitempty" mapstructure:"entity_version,omitempty"`
//UseCategoriesMapping *bool `json:"use_categories_mapping,omitempty" mapstructure:"use_categories_mapping,omitempty"`
}

// MessageResource ...
type MessageResource struct {

// Custom key-value details relevant to the status.
Details map[string]string `json:"details,omitempty" mapstructure:"details,omitempty"`

// If state is ERROR, a message describing the error.
Message *string `json:"message" mapstructure:"message"`

// If state is ERROR, a machine-readable snake-cased *string.
Reason *string `json:"reason" mapstructure:"reason"`
}

type ProjectQuotaMetadata struct {
Kind *string `json:"kind,omitempty"`
ProjectReference *Reference `json:"project_reference,omitempty"`
UUID *string `json:"uuid,omitempty"`
}

type ProjectQuotaData struct {
Disk *int `json:"disk,omitempty"`
VCPU *int `json:"vcpu,omitempty"`
Memory *int `json:"memory,omitempty"`
}

type ProjectQuotaEntities struct {
Project *string `json:"project,omitempty"`
}

type ProjectQuotaResources struct {
Data *ProjectQuotaData `json:"data,omitempty"`
Entities *ProjectQuotaEntities `json:"entities,omitempty"`
Metadata *Metadata `json:"metadata,omitempty"`
UUID *string `json:"uuid,omitempty"`
State *string `json:"state,omitempty"`
EntityType *string `json:"entity_type,omitempty"`
}

type ProjectQuotaSpec struct {
Resources *ProjectQuotaResources `json:"resources,omitempty"`
}

// Project CALM Quota
type ProjectQuotaIntentInput struct {
Metadata *ProjectQuotaMetadata `json:"metadata,omitempty"`
Spec *ProjectQuotaSpec `json:"spec,omitempty"`
}

type ProjectQuotaStatus struct {
State *string `json:"state,omitempty"`
MessageList []*MessageResource `json:"message_list,omitempty"`
Resources *ProjectQuotaResources `json:"resources,omitempty"`
}

type ProjectQuotaIntentResponse struct {
Status *ProjectQuotaStatus `json:"status,omitempty"`
Metadata *Metadata `json:"metadata,omitempty"`
Spec *ProjectQuotaSpec `json:"spec,omitempty"`
}

type EnableProjectQuotaInput struct {
Spec *ProjectQuotaSpec `json:"spec,omitempty"`
}
1 change: 1 addition & 0 deletions examples/foundationCentral/imaged_nodes/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ output "img1"{
// datasource to Get the details of a single node given its UUID.
data "nutanix_foundation_central_imaged_node_details" "imgdet"{
imaged_node_uuid = "<imaged_node_uuid>"
}

output "imgdetails"{
value = data.nutanix_foundation_central_imaged_node_details.imgdet
Expand Down
7 changes: 7 additions & 0 deletions examples/projects/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -102,4 +102,11 @@ resource "nutanix_project" "testp1" {
description= "descripton"
}
api_version = "3.1"

# to enable project quotas
project_quota{
vcpu = 1
disk = 2147483648
memory = 2147483648
}
}
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ require (
github.com/client9/misspell v0.3.4
github.com/golang/snappy v0.0.1 // indirect
github.com/golangci/golangci-lint v1.25.0
github.com/hashicorp/go-uuid v1.0.3 // indirect
github.com/hashicorp/terraform-plugin-go v0.5.0 // indirect
github.com/hashicorp/terraform-plugin-sdk/v2 v2.10.1
github.com/keybase/go-crypto v0.0.0-20161004153544-93f5b35093ba // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,8 @@ github.com/hashicorp/go-uuid v1.0.1 h1:fv1ep09latC32wFoVwnqcnKJGnMSdBanPczbHAYm1
github.com/hashicorp/go-uuid v1.0.1/go.mod h1:6SBZvOh/SIDV7/2o3Jml5SYk/TvGqwFJ/bN7x4byOro=
github.com/hashicorp/go-uuid v1.0.2 h1:cfejS+Tpcp13yd5nYHWDI6qVCny6wyX2Mt5SGur2IGE=
github.com/hashicorp/go-uuid v1.0.2/go.mod h1:6SBZvOh/SIDV7/2o3Jml5SYk/TvGqwFJ/bN7x4byOro=
github.com/hashicorp/go-uuid v1.0.3 h1:2gKiV6YVmrJ1i2CKKa9obLvRieoRGviZFL26PcT/Co8=
github.com/hashicorp/go-uuid v1.0.3/go.mod h1:6SBZvOh/SIDV7/2o3Jml5SYk/TvGqwFJ/bN7x4byOro=
github.com/hashicorp/go-version v1.0.0/go.mod h1:fltr4n8CU8Ke44wwGCBoEymUuxUHl09ZGVZPK5anwXA=
github.com/hashicorp/go-version v1.1.0/go.mod h1:fltr4n8CU8Ke44wwGCBoEymUuxUHl09ZGVZPK5anwXA=
github.com/hashicorp/go-version v1.2.0 h1:3vNe/fWF5CBgRIguda1meWhsZHy3m8gCJ5wx+dIzX/E=
Expand Down
7 changes: 7 additions & 0 deletions nutanix/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"

"github.com/terraform-providers/terraform-provider-nutanix/client"
"github.com/terraform-providers/terraform-provider-nutanix/client/calm"
foundation_central "github.com/terraform-providers/terraform-provider-nutanix/client/fc"
"github.com/terraform-providers/terraform-provider-nutanix/client/foundation"
"github.com/terraform-providers/terraform-provider-nutanix/client/karbon"
Expand Down Expand Up @@ -60,12 +61,17 @@ func (c *Config) Client() (*Client, error) {
if err != nil {
return nil, err
}
calmClient, err := calm.NewV3Client(configCreds)
if err != nil {
return nil, err
}
return &Client{
WaitTimeout: c.WaitTimeout,
API: v3Client,
KarbonAPI: karbonClient,
FoundationClientAPI: foundationClient,
FoundationCentral: fcClient,
Calm: calmClient,
}, nil
}

Expand All @@ -76,4 +82,5 @@ type Client struct {
FoundationClientAPI *foundation.Client
WaitTimeout int64
FoundationCentral *foundation_central.Client
Calm *calm.Client
}