Skip to content

Commit

Permalink
release v0.1.26
Browse files Browse the repository at this point in the history
  • Loading branch information
contabo committed Mar 21, 2024
1 parent 7eec7b9 commit d099c40
Show file tree
Hide file tree
Showing 38 changed files with 1,178 additions and 48 deletions.
78 changes: 49 additions & 29 deletions contabo/data_source_object_storage.go
Expand Up @@ -16,7 +16,7 @@ func dataSourceObjectStorage() *schema.Resource {
Schema: map[string]*schema.Schema{
"id": {
Type: schema.TypeString,
Required: true,
Optional: true,
Description: "The identifier of the Object Storage. Use it to manage it!",
},
"created_date": {
Expand Down Expand Up @@ -46,26 +46,26 @@ func dataSourceObjectStorage() *schema.Resource {
},
"auto_scaling": {
Type: schema.TypeList,
Computed: true,
Computed: true,
Optional: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"state": {
Type: schema.TypeString,
Computed: true,
Optional: true,
Optional: true,
Description: "Status of this object storage. It can be set to `enabled`, `disabled` or `error`.",
},
"size_limit_tb": {
Type: schema.TypeFloat,
Computed: true,
Optional: true,
Optional: true,
Description: "Autoscaling size limit for the current object storage.",
},
"error_message": {
Type: schema.TypeString,
Computed: true,
Optional: true,
Optional: true,
Description: "If the autoscaling is in an error state (see status property), the error message can be seen in this field.",
},
},
Expand Down Expand Up @@ -98,8 +98,8 @@ func dataSourceObjectStorage() *schema.Resource {
},
"display_name": {
Type: schema.TypeString,
Computed: true,
Description: "Display name for object storage.",
Optional: true,
Description: "Display name for object storage. Use it to manage it!",
},
},
}
Expand All @@ -111,33 +111,53 @@ func dataSourceObjectStorageRead(
m interface{},
) diag.Diagnostics {
var diags diag.Diagnostics

client := m.(*apiClient.APIClient)

var objectStorageId string
var err error
id := d.Get("id").(string)
if id != "" {
objectStorageId = id
}
objectStorageId := d.Get("id").(string)
objectStorageDisplayName := d.Get("display_name").(string)

if err != nil {
return diag.FromErr(err)
}
if objectStorageId == "" && objectStorageDisplayName == "" {
return HandleMissingDataObjectsFilters(diags, "Missing required field", "You must provide either the `id` or `display_name` field.")
} else if objectStorageId != "" && objectStorageDisplayName != "" {
return HandleMissingDataObjectsFilters(diags, "Multiple filters provided", "You must provide only one of the following fields: `id` or `display_name`.")
} else if (objectStorageId != "") {
res, httpResp, err := client.ObjectStoragesApi.RetrieveObjectStorage(ctx, objectStorageId).XRequestId(uuid.NewV4().String()).Execute()

res, httpResp, err := client.ObjectStoragesApi.RetrieveObjectStorage(ctx, objectStorageId).XRequestId(uuid.NewV4().String()).
Execute()
if err != nil {
return HandleResponseErrors(diags, httpResp)
}

if err != nil {
return HandleResponseErrors(diags, httpResp)
} else if len(res.Data) != 1 {
return MultipleDataObjectsError(diags)
}
if(len(res.Data) == 0) {
return NoDataError(diags)
}

d.SetId(res.Data[0].ObjectStorageId)
return AddObjectStorageToData(
res.Data[0],
d,
diags,
)
} else if (objectStorageDisplayName != "") {
res, httpResp, err := client.ObjectStoragesApi.RetrieveObjectStorageList(ctx).XRequestId(uuid.NewV4().String()).DisplayName(objectStorageDisplayName).Execute()

if err != nil {
return HandleResponseErrors(diags, httpResp)
}

d.SetId(res.Data[0].ObjectStorageId)
if(len(res.Data) == 0) {
return NoDataError(diags)
} else if(len(res.Data) > 1) {
return MultipleDataObjectsError(diags)
}

d.SetId(res.Data[0].ObjectStorageId)
return AddObjectStorageToData(
res.Data[0],
d,
diags,
)
}

return AddObjectStorageToData(
res.Data[0],
d,
diags,
)
return nil
}
69 changes: 69 additions & 0 deletions contabo/data_source_tag.go
@@ -0,0 +1,69 @@
package contabo

import (
"context"
"strconv"

apiClient "contabo.com/openapi"

"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
uuid "github.com/satori/go.uuid"
)

func dataSourceTag() *schema.Resource {
return &schema.Resource{
Description: "Tags are Customer-defined labels which can be attached to any resource in your account. Tag API represent simple CRUD functions and allow you to manage your tags. Use tags to group your resources. For example you can define some user group with tag and give them permission to create compute instance.",
ReadContext: dataSourceTagRead,
Schema: map[string]*schema.Schema{
"id": {
Type: schema.TypeString,
Required: true,
Description: "The identifier of the tag. Use it to manage it!",
},
"color": {
Type: schema.TypeString,
Computed: true,
Optional: true,
Description: "The tag color.",
},
"name": {
Type: schema.TypeString,
Computed: true,
Optional: true,
Description: "The tag name.",
},
},
}
}

func dataSourceTagRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
var diags diag.Diagnostics
client := m.(*apiClient.APIClient)

var tagId int64
var err error
id := d.Get("id").(string)
if id != "" {
tagId, err = strconv.ParseInt(id, 10, 64)
}

if err != nil {
return diag.FromErr(err)
}

res, httpResp, err := client.TagsApi.
RetrieveTag(ctx, tagId).
XRequestId(uuid.NewV4().String()).
Execute()

if err != nil {
return HandleResponseErrors(diags, httpResp)
} else if len(res.Data) != 1 {
return MultipleDataObjectsError(diags)
}

d.SetId(strconv.Itoa(int(res.Data[0].TagId)))

return AddTagToData(res.Data[0], d, diags)
}
79 changes: 79 additions & 0 deletions contabo/data_source_tag_assignment.go
@@ -0,0 +1,79 @@
package contabo

import (
"context"

apiClient "contabo.com/openapi"

"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
uuid "github.com/satori/go.uuid"
)

func dataSourceTagAssignment() *schema.Resource {
return &schema.Resource{
Description: "Tag assignment marks the specified resource with the specified tag for organizing purposes or to restrict access to that resource.",
ReadContext: dataSourceTagAssignmentRead,
Schema: map[string]*schema.Schema{
"id": {
Type: schema.TypeString,
Required: true,
Description: "The identifier of the tag assignment. Use it to manage it!",
},
"tag_id": {
Type: schema.TypeInt,
Computed: true,
Optional: true,
Description: "The identifier of the tag.",
},
"tag_name": {
Type: schema.TypeString,
Computed: true,
Description: "The name of the tag.",
},
"resource_type": {
Type: schema.TypeString,
Computed: true,
Optional: true,
Description: "The resource type.",
},
"resource_id": {
Type: schema.TypeString,
Computed: true,
Optional: true,
Description: "The resource id.",
},
"resource_name": {
Type: schema.TypeString,
Computed: true,
Description: "The resource name.",
},
},
}
}

func dataSourceTagAssignmentRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
var diags diag.Diagnostics
client := m.(*apiClient.APIClient)

id := d.Get("id").(string)
tagId := getTagIdFromId(id)
resourceType := getResourceTypeFromId(id)
resourceId := getResourceIdFromId(id)

res, httpResp, err := client.TagAssignmentsApi.
RetrieveAssignment(context.Background(), tagId, resourceType, resourceId).
XRequestId(uuid.NewV4().String()).Execute()
if err != nil {
return HandleResponseErrors(diags, httpResp)
}

if len(res.Data) == 0 {
return NoDataError(diags)
} else if len(res.Data) > 1 {
return MultipleDataObjectsError(diags)
}
d.SetId(id)
return AddTagAssignmentToData(res.Data[0], d, diags)

}
22 changes: 22 additions & 0 deletions contabo/handleErrors.go
Expand Up @@ -63,6 +63,28 @@ func MultipleDataObjectsError(
})
}

func NoDataError(
diags diag.Diagnostics,
) diag.Diagnostics {
return append(diags, diag.Diagnostic{
Severity: diag.Error,
Summary: "API response returned empty data.",
Detail: "The API response returned empty data.",
})
}

func HandleMissingDataObjectsFilters(
diags diag.Diagnostics,
summary string,
details string,
) diag.Diagnostics {
return append(diags, diag.Diagnostic{
Severity: diag.Error,
Summary: summary,
Detail: details,
})
}

func HandleDownloadErrors(
diags diag.Diagnostics,
) diag.Diagnostics {
Expand Down
4 changes: 4 additions & 0 deletions contabo/provider.go
Expand Up @@ -65,6 +65,8 @@ func Provider() *schema.Provider {
"contabo_secret": resourceSecret(),
"contabo_private_network": resourcePrivateNetwork(),
"contabo_object_storage_bucket": resourceObjectStorageBucket(),
"contabo_tag": resourceTag(),
"contabo_tag_assignment": resourceTagAssignment(),
},
DataSourcesMap: map[string]*schema.Resource{
"contabo_instance": dataSourceInstance(),
Expand All @@ -74,6 +76,8 @@ func Provider() *schema.Provider {
"contabo_secret": dataSourceSecret(),
"contabo_private_network": dataSourcePrivateNetwork(),
"contabo_object_storage_bucket": dataSourceObjectStorageBucket(),
"contabo_tag": dataSourceTag(),
"contabo_tag_assignment": dataSourceTagAssignment(),
},
ConfigureContextFunc: providerConfigure,
}
Expand Down

0 comments on commit d099c40

Please sign in to comment.