Skip to content

Add cloudstack_limits data source and resource #197

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

Open
wants to merge 2 commits into
base: main
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
210 changes: 210 additions & 0 deletions cloudstack/data_source_cloudstack_limits.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,210 @@
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
//

package cloudstack

import (
"bytes"
"crypto/sha256"
"encoding/hex"
"fmt"

"github.com/apache/cloudstack-go/v2/cloudstack"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
)

func dataSourceCloudStackLimits() *schema.Resource {
return &schema.Resource{
Read: dataSourceCloudStackLimitsRead,
Schema: map[string]*schema.Schema{
"type": {
Type: schema.TypeString,
Optional: true,
ValidateFunc: validation.StringInSlice([]string{
"instance", "ip", "volume", "snapshot", "template", "project", "network", "vpc",
"cpu", "memory", "primarystorage", "secondarystorage", "publicip", "eip", "autoscalevmgroup",
}, false), // false disables case-insensitive matching
Description: "The type of resource to list the limits. Available types are: " +
"instance, ip, volume, snapshot, template, project, network, vpc, cpu, memory, " +
"primarystorage, secondarystorage, publicip, eip, autoscalevmgroup",
},
"account": {
Type: schema.TypeString,
Optional: true,
Description: "List resources by account. Must be used with the domainid parameter.",
},
"domainid": {
Type: schema.TypeString,
Optional: true,
Description: "List only resources belonging to the domain specified.",
},
"projectid": {
Type: schema.TypeString,
Optional: true,
Description: "List resource limits by project.",
},
"limits": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"resourcetype": {
Type: schema.TypeString,
Computed: true,
},
"resourcetypename": {
Type: schema.TypeString,
Computed: true,
},
"account": {
Type: schema.TypeString,
Computed: true,
},
"domain": {
Type: schema.TypeString,
Computed: true,
},
"domainid": {
Type: schema.TypeString,
Computed: true,
},
"max": {
Type: schema.TypeInt,
Computed: true,
},
"project": {
Type: schema.TypeString,
Computed: true,
},
"projectid": {
Type: schema.TypeString,
Computed: true,
},
},
},
},
},
}
}

func dataSourceCloudStackLimitsRead(d *schema.ResourceData, meta interface{}) error {
cs := meta.(*cloudstack.CloudStackClient)

// Create a new parameter struct
p := cs.Limit.NewListResourceLimitsParams()

// Set optional parameters
if v, ok := d.GetOk("type"); ok {
typeStr := v.(string)
if resourcetype, ok := resourceTypeMap[typeStr]; ok {
p.SetResourcetype(resourcetype)
} else {
return fmt.Errorf("invalid type value: %s", typeStr)
}
}

if v, ok := d.GetOk("account"); ok {
p.SetAccount(v.(string))
}

if v, ok := d.GetOk("domainid"); ok {
p.SetDomainid(v.(string))
}

if v, ok := d.GetOk("projectid"); ok {
p.SetProjectid(v.(string))
}

// Retrieve the resource limits
l, err := cs.Limit.ListResourceLimits(p)
if err != nil {
return fmt.Errorf("Error retrieving resource limits: %s", err)
}

// Generate a unique ID for this data source
id := generateDataSourceID(d)
d.SetId(id)

limits := make([]map[string]interface{}, 0, len(l.ResourceLimits))

// Set the resource data
for _, limit := range l.ResourceLimits {
limitMap := map[string]interface{}{
"resourcetype": limit.Resourcetype,
"resourcetypename": limit.Resourcetypename,
"max": limit.Max,
}

if limit.Account != "" {
limitMap["account"] = limit.Account
}

if limit.Domain != "" {
limitMap["domain"] = limit.Domain
}

if limit.Domainid != "" {
limitMap["domainid"] = limit.Domainid
}

if limit.Project != "" {
limitMap["project"] = limit.Project
}

if limit.Projectid != "" {
limitMap["projectid"] = limit.Projectid
}

limits = append(limits, limitMap)
}

if err := d.Set("limits", limits); err != nil {
return fmt.Errorf("Error setting limits: %s", err)
}

return nil
}

// generateDataSourceID generates a unique ID for the data source based on its parameters
func generateDataSourceID(d *schema.ResourceData) string {
var buf bytes.Buffer

if v, ok := d.GetOk("type"); ok {
typeStr := v.(string)
if resourcetype, ok := resourceTypeMap[typeStr]; ok {
buf.WriteString(fmt.Sprintf("%d-", resourcetype))
}
}

if v, ok := d.GetOk("account"); ok {
buf.WriteString(fmt.Sprintf("%s-", v.(string)))
}

if v, ok := d.GetOk("domainid"); ok {
buf.WriteString(fmt.Sprintf("%s-", v.(string)))
}

if v, ok := d.GetOk("projectid"); ok {
buf.WriteString(fmt.Sprintf("%s-", v.(string)))
}

// Generate a SHA-256 hash of the buffer content
hash := sha256.Sum256(buf.Bytes())
return fmt.Sprintf("limits-%s", hex.EncodeToString(hash[:])[:8])
}
2 changes: 2 additions & 0 deletions cloudstack/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ func Provider() *schema.Provider {
"cloudstack_user": dataSourceCloudstackUser(),
"cloudstack_vpn_connection": dataSourceCloudstackVPNConnection(),
"cloudstack_pod": dataSourceCloudstackPod(),
"cloudstack_limits": dataSourceCloudStackLimits(),
},

ResourcesMap: map[string]*schema.Resource{
Expand All @@ -105,6 +106,7 @@ func Provider() *schema.Provider {
"cloudstack_ipaddress": resourceCloudStackIPAddress(),
"cloudstack_kubernetes_cluster": resourceCloudStackKubernetesCluster(),
"cloudstack_kubernetes_version": resourceCloudStackKubernetesVersion(),
"cloudstack_limits": resourceCloudStackLimits(),
"cloudstack_loadbalancer_rule": resourceCloudStackLoadBalancerRule(),
"cloudstack_network": resourceCloudStackNetwork(),
"cloudstack_network_acl": resourceCloudStackNetworkACL(),
Expand Down
Loading