-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
contabo
committed
Mar 21, 2024
1 parent
7eec7b9
commit d099c40
Showing
38 changed files
with
1,178 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.