Skip to content

Commit

Permalink
feat: add rooms support
Browse files Browse the repository at this point in the history
  • Loading branch information
witalisoft committed Jan 17, 2024
1 parent af1d316 commit d18731e
Show file tree
Hide file tree
Showing 20 changed files with 633 additions and 54 deletions.
34 changes: 0 additions & 34 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ This provider allows you to install and manage Netdata Cloud resources using Ter

* [Requirements](#requirements)
* [Getting Started](#getting-started)
* [Example](#example)

## Requirements

Expand Down Expand Up @@ -42,36 +41,3 @@ TODO
```console
$ make local-build
```

## Example

```hcl
terraform {
required_providers {
netdata = {
# TODO: Update this string with the published name of your provider.
source = "netdata.cloud/todo/netdata"
}
}
required_version = ">= 1.1.0"
}
provider "netdata" {
url = "https://app.netdata.cloud"
authtoken = "<authtoken>"
}
resource "netdata_space" "test" {
name = "MyTestingSpace"
description = "Created by Terraform"
}
data "netdata_space" "test" {
id = "ee3ec76d-0180-4ef4-93ae-c94c1e7ed2f1"
}
output "datasource" {
value = data.netdata_space.test.name
}
```
33 changes: 33 additions & 0 deletions docs/data-sources/room.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
---
# generated by https://github.com/hashicorp/terraform-plugin-docs
page_title: "netdata_room Data Source - terraform-provider-netdata"
subcategory: ""
description: |-
---

# netdata_room (Data Source)



## Example Usage

```terraform
data "netdata_room" "test" {
spaceid = "<spaceid>"
id = "<roomid>"
}
```

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

### Required

- `id` (String) The ID of the room
- `spaceid` (String) The ID of the space

### Read-Only

- `description` (String) The description of the room
- `name` (String) The name of the room
2 changes: 1 addition & 1 deletion docs/data-sources/space.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ description: |-

```terraform
data "netdata_space" "test" {
id = "ee3ec76d-0180-4ef4-93ae-c94c1e7ed2f1"
id = "<spaceid>"
}
```

Expand Down
47 changes: 47 additions & 0 deletions docs/resources/room.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
---
# generated by https://github.com/hashicorp/terraform-plugin-docs
page_title: "netdata_room Resource - terraform-provider-netdata"
subcategory: ""
description: |-
---

# netdata_room (Resource)



## Example Usage

```terraform
resource "netdata_room" "test" {
spaceid = "<spaceid>"
name = "MyTestingSpace"
description = "Created by Terraform"
}
```

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

### Required

- `name` (String) The name of the room
- `spaceid` (String) The ID of the space

### Optional

- `description` (String) The description of the room

### Read-Only

- `id` (String) The ID of the room

## Import

Import is supported using the following syntax:

```shell
#!/bin/sh

terraform import netdata_room.test spaceid,roomid
```
2 changes: 1 addition & 1 deletion docs/resources/space.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,5 +41,5 @@ Import is supported using the following syntax:
```shell
#!/bin/sh

terraform import netdata_space.test ee3ec76d-0180-4ef4-93ae-c94c1e7ed2f1
terraform import netdata_space.test spaceid
```
4 changes: 4 additions & 0 deletions examples/data-sources/netdata_room/data-source.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
data "netdata_room" "test" {
spaceid = "<spaceid>"
id = "<roomid>"
}
2 changes: 1 addition & 1 deletion examples/data-sources/netdata_space/data-source.tf
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
data "netdata_space" "test" {
id = "ee3ec76d-0180-4ef4-93ae-c94c1e7ed2f1"
id = "<spaceid>"
}
3 changes: 3 additions & 0 deletions examples/resources/netdata_room/import.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/bin/sh

terraform import netdata_room.test spaceid,roomid
5 changes: 5 additions & 0 deletions examples/resources/netdata_room/resource.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
resource "netdata_room" "test" {
spaceid = "<spaceid>"
name = "MyTestingSpace"
description = "Created by Terraform"
}
2 changes: 1 addition & 1 deletion examples/resources/netdata_space/import.sh
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
#!/bin/sh

terraform import netdata_space.test ee3ec76d-0180-4ef4-93ae-c94c1e7ed2f1
terraform import netdata_space.test spaceid
14 changes: 13 additions & 1 deletion examples/spaces/main.tf
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
terraform {
required_providers {
netdata = {
# TODO: Update this string with the published name of your provider.
source = "netdata.cloud/todo/netdata"
}
}
Expand All @@ -17,8 +18,19 @@ resource "netdata_space" "test" {
description = "Created by Terraform"
}

resource "netdata_room" "test" {
spaceid = netdata_space.test.id
name = "MyTestingRoom"
description = "Created by Terraform2"
}

data "netdata_space" "test" {
id = "ee3ec76d-0180-4ef4-93ae-c94c1e7ed2f1"
id = netdata_space.test.id
}

data "netdata_room" "test" {
id = netdata_room.test.id
spaceid = netdata_space.test.id
}

output "datasource" {
Expand Down
6 changes: 6 additions & 0 deletions internal/client/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,9 @@ type SpaceInfo struct {
Name string `json:"name"`
Description string `json:"description"`
}

type RoomInfo struct {
ID string `json:"id"`
Name string `json:"name"`
Description string `json:"description"`
}
123 changes: 123 additions & 0 deletions internal/client/rooms.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
package client

import (
"encoding/json"
"fmt"
"net/http"
"strings"
)

func (c *Client) GetRooms(spaceid string) (*[]RoomInfo, error) {
if spaceid == "" {
return nil, fmt.Errorf("spaceid is empty")
}
req, err := http.NewRequest("GET", fmt.Sprintf("%s/api/v2/spaces/%s/rooms", c.HostURL, spaceid), nil)
if err != nil {
return nil, err
}

body, err := c.doRequest(req)
if err != nil {
return nil, err
}

var rooms []RoomInfo
err = json.Unmarshal(body, &rooms)
if err != nil {
return nil, err
}

return &rooms, nil
}

func (c *Client) GetRoomByID(id, spaceid string) (*RoomInfo, error) {
rooms, err := c.GetRooms(spaceid)
if err != nil {
return nil, err
}
for _, room := range *rooms {
if room.ID == id {
return &room, nil
}
}
return nil, ErrNotFound
}

func (c *Client) CreateRoom(spaceid, name, description string) (*RoomInfo, error) {
if spaceid == "" {
return nil, fmt.Errorf("spaceid is empty")
}
reqBody, err := json.Marshal(map[string]string{
"name": name,
"description": description,
})
if err != nil {
return nil, err
}

req, err := http.NewRequest("POST", fmt.Sprintf("%s/api/v1/spaces/%s/rooms", c.HostURL, spaceid), strings.NewReader(string(reqBody)))
if err != nil {
return nil, err
}

respBody, err := c.doRequest(req)
if err != nil {
return nil, err
}

var room RoomInfo
err = json.Unmarshal(respBody, &room)
if err != nil {
return nil, err
}

room.Name = name
room.Description = description

return &room, nil
}

func (c *Client) UpdateRoomByID(id, spaceid, name, description string) error {
if id == "" {
return fmt.Errorf("id is empty")
}
if spaceid == "" {
return fmt.Errorf("spaceid is empty")
}
reqBody, err := json.Marshal(map[string]string{
"name": name,
"description": description,
})
if err != nil {
return err
}
req, err := http.NewRequest("PATCH", fmt.Sprintf("%s/api/v1/spaces/%s/rooms/%s", c.HostURL, spaceid, id), strings.NewReader(string(reqBody)))
if err != nil {
return err
}
_, err = c.doRequest(req)
if err != nil {
return err
}
return nil
}

func (c *Client) DeleteRoomByID(id, spaceid string) error {
if id == "" {
return fmt.Errorf("id is empty")
}
if spaceid == "" {
return fmt.Errorf("spaceid is empty")
}
req, err := http.NewRequest("DELETE", fmt.Sprintf("%s/api/v1/spaces/%s/rooms/%s", c.HostURL, spaceid, id), nil)
if err != nil {
return err
}

_, err = c.doRequest(req)
if err != nil {
return err
}

return nil
}
2 changes: 1 addition & 1 deletion internal/client/spaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ func (c *Client) UpdateSpaceByID(id, name, description string) error {
}
req, err := http.NewRequest("PATCH", fmt.Sprintf("%s/api/v1/spaces/%s", c.HostURL, id), strings.NewReader(string(reqBody)))
if err != nil {
return fmt.Errorf("req %+v", req)
return err
}
_, err = c.doRequest(req)
if err != nil {
Expand Down
2 changes: 2 additions & 0 deletions internal/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,12 +88,14 @@ func (p *netdataCloudProvider) Configure(ctx context.Context, req provider.Confi
func (p *netdataCloudProvider) Resources(ctx context.Context) []func() resource.Resource {
return []func() resource.Resource{
NewSpaceResource,
NewRoomResource,
}
}

func (p *netdataCloudProvider) DataSources(ctx context.Context) []func() datasource.DataSource {
return []func() datasource.DataSource{
NewSpaceDataSource,
NewRoomDataSource,
}
}

Expand Down
Loading

0 comments on commit d18731e

Please sign in to comment.