Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
witalisoft committed Jan 16, 2024
1 parent 066ebcd commit af1d316
Show file tree
Hide file tree
Showing 11 changed files with 181 additions and 15 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Netdata Cloud provider for Terraform
# Terraform Provider for Netdata Cloud

This is the [Netdata Cloud](https://www.netdata.cloud/) provider for [Terraform](https://www.terraform.io/).
This is the [Terraform](https://www.terraform.io/) provider for the [Netdata Cloud](https://www.netdata.cloud/).

This provider allows you to install and manage Netdata Cloud resources using Terraform.

Expand All @@ -24,7 +24,7 @@ TODO

* from source code

* setup your CLI configuration
* setup your [CLI configuration](https://developer.hashicorp.com/terraform/cli/config/config-file#development-overrides-for-provider-developers)

```console
$ cat ~/.terraformrc
Expand Down Expand Up @@ -58,7 +58,7 @@ terraform {
provider "netdata" {
url = "https://app.netdata.cloud"
authtoken = "authtoken"
authtoken = "<authtoken>"
}
resource "netdata_space" "test" {
Expand Down
7 changes: 2 additions & 5 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,14 @@ description: |-
```terraform
provider "netdata" {
url = "https://app.netdata.cloud"
authtoken = "authtoken"
authtoken = "<authtoken>"
}
```

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

### Required

- `authtoken` (String, Sensitive) Netdata Cloud Authentication Token. Can be also set as environment variable `NETDATA_CLOUD_AUTH_TOKEN`

### Optional

- `authtoken` (String, Sensitive) Netdata Cloud Authentication Token. Can be also set as environment variable `NETDATA_CLOUD_AUTH_TOKEN`
- `url` (String) Netdata Cloud URL Address by default is https://app.netdata.cloud. Can be also set as environment variable `NETDATA_CLOUD_URL`
2 changes: 1 addition & 1 deletion docs/resources/space.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ resource "netdata_space" "test" {
Import is supported using the following syntax:

```shell
#!/bin/sh
#!/bin/sh

terraform import netdata_space.test ee3ec76d-0180-4ef4-93ae-c94c1e7ed2f1
```
2 changes: 1 addition & 1 deletion examples/provider/provider.tf
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
provider "netdata" {
url = "https://app.netdata.cloud"
authtoken = "authtoken"
authtoken = "<authtoken>"
}
2 changes: 1 addition & 1 deletion examples/spaces/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ terraform {

provider "netdata" {
url = "https://app.netdata.cloud"
authtoken = "authtoken"
authtoken = "<authtoken>"
}

resource "netdata_space" "test" {
Expand Down
50 changes: 50 additions & 0 deletions internal/client/client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package client

import (
"errors"
"fmt"
"io"
"net/http"
"time"
)

var ErrNotFound = errors.New("not found")

type Client struct {
HostURL string
HTTPClient *http.Client
AuthToken string
}

func NewClient(url, authtoken string) *Client {
c := Client{
HostURL: url,
AuthToken: "Bearer " + authtoken,
HTTPClient: &http.Client{Timeout: 10 * time.Second},
}

return &c
}

func (c *Client) doRequest(req *http.Request) ([]byte, error) {
req.Header.Set("Authorization", c.AuthToken)
req.Header.Set("Accept", "application/json")

res, err := c.HTTPClient.Do(req)
if err != nil {
return nil, err
}
defer res.Body.Close()

body, err := io.ReadAll(res.Body)
if err != nil {
return nil, err
}

statusOK := res.StatusCode >= 200 && res.StatusCode < 300
if !statusOK {
return nil, fmt.Errorf("status: %d, body: %s", res.StatusCode, body)
}

return body, err
}
7 changes: 7 additions & 0 deletions internal/client/models.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package client

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

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

func (c *Client) GetSpaces() (*[]SpaceInfo, error) {
req, err := http.NewRequest("GET", fmt.Sprintf("%s/api/v3/spaces", c.HostURL), nil)
if err != nil {
return nil, err
}

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

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

return &spaces, nil
}

func (c *Client) GetSpaceByID(id string) (*SpaceInfo, error) {
spaces, err := c.GetSpaces()
if err != nil {
return nil, err
}
for _, space := range *spaces {
if space.ID == id {
return &space, nil
}
}
return nil, ErrNotFound
}

func (c *Client) CreateSpace(name, description string) (*SpaceInfo, error) {
reqBody, err := json.Marshal(map[string]string{"name": name})
if err != nil {
return nil, err
}

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

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

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

err = c.UpdateSpaceByID(space.ID, name, description)
if err != nil {
return nil, err
}
space.Name = name
space.Description = description

return &space, nil
}

func (c *Client) UpdateSpaceByID(id, name, description string) error {
if id == "" {
return fmt.Errorf("id 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", c.HostURL, id), strings.NewReader(string(reqBody)))
if err != nil {
return fmt.Errorf("req %+v", req)
}
_, err = c.doRequest(req)
if err != nil {
return err
}
return nil
}

func (c *Client) DeleteSpaceByID(id string) error {
if id == "" {
return fmt.Errorf("id is empty")
}
req, err := http.NewRequest("DELETE", fmt.Sprintf("%s/api/v1/spaces/%s", c.HostURL, 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/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"github.com/hashicorp/terraform-plugin-framework/provider/schema"
"github.com/hashicorp/terraform-plugin-framework/resource"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/netdata/terraform-provider-netdata/pkg/client"
"github.com/netdata/terraform-provider-netdata/internal/client"
)

var _ provider.Provider = &netdataCloudProvider{}
Expand Down
2 changes: 1 addition & 1 deletion internal/provider/space_data_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"github.com/hashicorp/terraform-plugin-framework/datasource"
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/netdata/terraform-provider-netdata/pkg/client"
"github.com/netdata/terraform-provider-netdata/internal/client"
)

var (
Expand Down
2 changes: 1 addition & 1 deletion internal/provider/space_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
"github.com/hashicorp/terraform-plugin-framework/resource/schema/stringplanmodifier"
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/netdata/terraform-provider-netdata/pkg/client"
"github.com/netdata/terraform-provider-netdata/internal/client"
)

var (
Expand Down

0 comments on commit af1d316

Please sign in to comment.