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

Resource group #36

Merged
merged 12 commits into from
Feb 3, 2025
Merged
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
62 changes: 62 additions & 0 deletions docs/resources/resource_group.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
---
# generated by https://github.com/hashicorp/terraform-plugin-docs
page_title: "trocco_resource_group Resource - trocco"
subcategory: ""
description: |-
Provides a TROCCO resource_group resource.
---

# trocco_resource_group (Resource)

Provides a TROCCO resource_group resource.

## Example Usage

```terraform
resource "trocco_resource_group" "example" {
name = "resource group name"
description = "description"
teams = [
{
team_id = 1
role = "administrator"
},
{
team_id = 2
role = "operator"
}
]
}
```

<!-- schema generated by tfplugindocs -->
## Schema

### Required

- `name` (String) The name of the resource group.
- `teams` (Attributes Set) The team roles of the resource group. (see [below for nested schema](#nestedatt--teams))

### Optional

- `description` (String) The description of the resource group.

### Read-Only

- `id` (Number) The ID of the resource group.

<a id="nestedatt--teams"></a>
### Nested Schema for `teams`

Required:

- `role` (String) The role of the team. Valid values are `administrator`, `editor`, `operator`, `viewer`.
- `team_id` (Number) The team ID of the role.

## Import

Import is supported using the following syntax:

```shell
terraform import trocco_resource.import_resource_group <resource_group_id>
```
1 change: 1 addition & 0 deletions examples/resources/trocco_resource_group/import.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
terraform import trocco_resource.import_resource_group <resource_group_id>
15 changes: 15 additions & 0 deletions examples/resources/trocco_resource_group/resource.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
resource "trocco_resource_group" "example" {
name = "resource group name"
description = "description"
teams = [
{
team_id = 1
role = "administrator"
},
{
team_id = 2
role = "operator"
}
]
}

124 changes: 124 additions & 0 deletions internal/client/resource_group.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
package client

import (
"fmt"
"net/http"
"net/url"
)

const resourceGroupBasePath = "/api/resource_groups"

type ResourceGroupWithTeams struct {
ID int64 `json:"id"`
Name string `json:"name"`
Description *string `json:"description"`
Teams []ResourceGroupPermission `json:"teams"`
CreatedAt string `json:"created_at"`
UpdatedAt string `json:"updated_at"`
}

type ResourceGroupPermission struct {
TeamID int64 `json:"team_id"`
Role string `json:"role"`
}

// List of ResourceGroups

type ListResourceGroupInput struct {
limit *int
cursor *string
}

func (input *ListResourceGroupInput) SetLimit(limit int) {
input.limit = &limit
}

func (input *ListResourceGroupInput) SetCursor(cursor string) {
input.cursor = &cursor
}

type ListResourceGroupOutput struct {
Items []ResourceGroupWithTeams `json:"items"`
NextCursor *string `json:"next_cursor"`
}

const MaxListResourceGroupsLimit = 100

func (client *TroccoClient) ListResourceGroups(input *ListResourceGroupInput) (*ListResourceGroupOutput, error) {
params := url.Values{}
if input != nil && input.limit != nil {
if *input.limit < 1 || *input.limit > MaxListResourceGroupsLimit {
return nil, fmt.Errorf("limit must be between 1 and %d", MaxListResourceGroupsLimit)
}
params.Add("limit", fmt.Sprintf("%d", *input.limit))
}
if input != nil && input.cursor != nil {
params.Add("cursor", *input.cursor)
}
path := fmt.Sprintf(resourceGroupBasePath+"?%s", params.Encode())
output := new(ListResourceGroupOutput)
err := client.do(http.MethodGet, path, nil, output)
if err != nil {
return nil, err
}
return output, nil
}

// Get a Team

func (client *TroccoClient) GetResourceGroup(id int64) (*ResourceGroupWithTeams, error) {
path := fmt.Sprintf("%s/%d", resourceGroupBasePath, id)
output := new(ResourceGroupWithTeams)
err := client.do(http.MethodGet, path, nil, output)
if err != nil {
return nil, err
}
return output, nil
}

// Create

type CreateResourceGroupInput struct {
Name string `json:"name"`
Description *string `json:"description,omitempty"`
Teams []TeamRoleInput `json:"teams"`
}

type TeamRoleInput struct {
TeamID int64 `json:"team_id"`
Role string `json:"role"`
}

func (client *TroccoClient) CreateResourceGroup(input *CreateResourceGroupInput) (*ResourceGroupWithTeams, error) {
output := new(ResourceGroupWithTeams)
err := client.do(http.MethodPost, resourceGroupBasePath, input, output)
if err != nil {
return nil, err
}
return output, nil
}

// Update

type UpdateResourceGroupInput struct {
Name *string `json:"name"`
Description *string `json:"description,omitempty"`
Teams []TeamRoleInput `json:"teams"`
}

func (client *TroccoClient) UpdateResourceGroup(id int64, input *UpdateResourceGroupInput) (*ResourceGroupWithTeams, error) {
path := fmt.Sprintf("%s/%d", resourceGroupBasePath, id)
output := new(ResourceGroupWithTeams)
err := client.do(http.MethodPatch, path, input, output)
if err != nil {
return nil, err
}
return output, nil
}

// Delete

func (client *TroccoClient) DeleteResourceGroup(id int64) error {
path := fmt.Sprintf("%s/%d", resourceGroupBasePath, id)
return client.do(http.MethodDelete, path, nil, nil)
}
Loading