Skip to content

Commit

Permalink
initial TF Provider code
Browse files Browse the repository at this point in the history
  • Loading branch information
pixie79 committed Apr 19, 2023
0 parents commit 9dd08a7
Show file tree
Hide file tree
Showing 8 changed files with 727 additions and 0 deletions.
21 changes: 21 additions & 0 deletions LICENSE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2023 Mark Olliver

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
94 changes: 94 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
# Terraform Red Panda Provider

This is a custom Terraform provider for managing topics and schemas in Red Panda.

## Prerequisites

- [Go](https://golang.org/dl/) >= 1.16
- [Terraform](https://www.terraform.io/downloads.html) >= 0.13.x
- [Red Panda](https://vectorized.io/redpanda/) >= 21.11.1

## Building the provider

1. Clone the repository:

```bash
git clone https://github.com/pixie79/terraform-provider-redpanda.git
cd terraform-provider-redpanda
```


2. Build the provider:

```bash
go build -o terraform-provider-redpanda
```


3. Install the provider:

```bash
mkdir -p ~/.terraform.d/plugins/example.com/local/redpanda/0.1.0/linux_amd64
mv terraform-provider-redpanda ~/.terraform.d/plugins/example.com/local/redpanda/0.1.0/linux_amd64/
```

Replace `linux_amd64` with the appropriate directory for your platform if needed.

## Using the provider

1. Create a `main.tf` file:

```hcl
terraform {
required_providers {
redpanda = {
source = "example.com/local/redpanda"
version = "0.1.0"
}
}
}
provider "redpanda" {
api_url = "http://localhost:8081"
}
resource "redpanda_topic" "example_topic" {
name = "example-topic"
partitions = 3
replication_factor = 1
}
resource "redpanda_schema" "example_schema" {
subject = "example-topic-value"
schema = "{\"type\": \"record\", \"name\": \"example\", \"fields\": [{\"name\": \"field1\", \"type\": \"string\"}]}"
schema_type = "AVRO"
}
```

Replace the api_url with the URL to your Red Panda schema registry.

2. Initialize Terraform:

```bash
terraform init
```

3. Apply the configuration:

```bash
terraform apply
```

4. To destroy the created resources:

```bash
terraform destroy
```


# License

This project is licensed under the MIT License.


This `README.md` file provides an overview of the Terraform Red Panda provider, including prerequisites, build instructions, installation, and usage.
243 changes: 243 additions & 0 deletions client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,243 @@
// client.go
package main

import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"net/http"
"bytes"
)

type Client struct {
APIURL string
}

func NewClient(apiURL string) *Client {
return &Client{
APIURL: apiURL,
}
}

type Topic struct {
Name string `json:"name"`
Partitions int `json:"partitions"`
ReplicationFactor int `json:"replication_factor"`
}

func (c *Client) CreateTopic(topic *Topic) error {
reqBody, err := json.Marshal(topic)
if err != nil {
return err
}

req, err := http.NewRequest("POST", fmt.Sprintf("%s/topics", c.APIURL), bytes.NewBuffer(reqBody))
if err != nil {
return err
}
req.Header.Set("Content-Type", "application/json")

resp, err := http.DefaultClient.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()

if resp.StatusCode != http.StatusCreated {
bodyBytes, _ := ioutil.ReadAll(resp.Body)
return errors.New(string(bodyBytes))
}

return nil
}

func (c *Client) GetTopic(topicName string) (*Topic, error) {
req, err := http.NewRequest("GET", fmt.Sprintf("%s/topics/%s", c.APIURL, topicName), nil)
if err != nil {
return nil, err
}
req.Header.Set("Content-Type", "application/json")

resp, err := http.DefaultClient.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()

if resp.StatusCode != http.StatusOK {
bodyBytes, _ := ioutil.ReadAll(resp.Body)
return nil, errors.New(string(bodyBytes))
}

var topic Topic
err = json.NewDecoder(resp.Body).Decode(&topic)
if err != nil {
return nil, err
}

return &topic, nil
}


func (c *Client) UpdateTopic(topic *Topic) error {
reqBody, err := json.Marshal(topic)
if err != nil {
return err
}

req, err := http.NewRequest("PUT", fmt.Sprintf("%s/topics/%s", c.APIURL, topic.Name), bytes.NewBuffer(reqBody))
if err != nil {
return err
}
req.Header.Set("Content-Type", "application/json")

resp, err := http.DefaultClient.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()

if resp.StatusCode != http.StatusOK {
bodyBytes, _ := ioutil.ReadAll(resp.Body)
return errors.New(string(bodyBytes))
}

return nil
}

func (c *Client) DeleteTopic(topicName string) error {
req, err := http.NewRequest("DELETE", fmt.Sprintf("%s/topics/%s", c.APIURL, topicName), nil)
if err != nil {
return err
}
req.Header.Set("Content-Type", "application/json")

resp, err := http.DefaultClient.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()

if resp.StatusCode != http.StatusNoContent {
bodyBytes, _ := ioutil.ReadAll(resp.Body)
return errors.New(string(bodyBytes))
}

return nil
}

type Schema struct {
Subject string `json:"subject"`
Schema string `json:"schema"`
SchemaType string `json:"schema_type,omitempty"`
Version int `json:"version,omitempty"`
Id int `json:"id,omitempty"`
}

func (c *Client) CreateSchema(schema *Schema) error {
reqBody, err := json.Marshal(schema)
if err != nil {
return err
}

req, err := http.NewRequest("POST", fmt.Sprintf("%s/subjects/%s/versions", c.APIURL, schema.Subject), bytes.NewBuffer(reqBody))
if err != nil {
return err
}
req.Header.Set("Content-Type", "application/json")

resp, err := http.DefaultClient.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()

if resp.StatusCode != http.StatusOK {
bodyBytes, _ := ioutil.ReadAll(resp.Body)
return errors.New(string(bodyBytes))
}

var schemaResponse struct {
Id int `json:"id"`
}
err = json.NewDecoder(resp.Body).Decode(&schemaResponse)
if err != nil {
return err
}

schema.Id = schemaResponse.Id

return nil
}


func (c *Client) GetSchema(subject string, version string) (*Schema, error) {
req, err := http.NewRequest("GET", fmt.Sprintf("%s/subjects/%s/versions/%s", c.APIURL, subject, version), nil)
if err != nil {
return nil, err
}
req.Header.Set("Content-Type", "application/json")

resp, err := http.DefaultClient.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()

if resp.StatusCode != http.StatusOK {
bodyBytes, _ := ioutil.ReadAll(resp.Body)
return nil, errors.New(string(bodyBytes))
}

var schemaResponse struct {
Subject string `json:"subject"`
Version int `json:"version"`
Id int `json:"id"`
Schema string `json:"schema"`
SchemaType string `json:"schemaType,omitempty"`
}

err = json.NewDecoder(resp.Body).Decode(&schemaResponse)
if err != nil {
return nil, err
}

schema := &Schema{
Subject: schemaResponse.Subject,
Version: schemaResponse.Version,
Id: schemaResponse.Id,
Schema: schemaResponse.Schema,
SchemaType: schemaResponse.SchemaType,
}

return schema, nil
}


func (c *Client) UpdateSchema(schema *Schema) error {
// Alias to CreateSchema as schemas are created as new versions
return c.CreateSchema(schema)
}


func (c *Client) DeleteSchema(subject string) error {
req, err := http.NewRequest("DELETE", fmt.Sprintf("%s/subjects/%s", c.APIURL, subject), nil)
if err != nil {
return err
}
req.Header.Set("Content-Type", "application/json")

resp, err := http.DefaultClient.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()

if resp.StatusCode != http.StatusOK {
bodyBytes, _ := ioutil.ReadAll(resp.Body)
return errors.New(string(bodyBytes))
}

return nil
}
38 changes: 38 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
module github.com/pixie79/terraform-provider-redpanda

go 1.20

require (
github.com/agext/levenshtein v1.2.2 // indirect
github.com/apparentlymart/go-textseg/v13 v13.0.0 // indirect
github.com/fatih/color v1.13.0 // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/google/go-cmp v0.5.9 // indirect
github.com/hashicorp/errwrap v1.0.0 // indirect
github.com/hashicorp/go-cty v1.4.1-0.20200414143053-d3edf31b6320 // indirect
github.com/hashicorp/go-hclog v1.4.0 // indirect
github.com/hashicorp/go-multierror v1.1.1 // indirect
github.com/hashicorp/go-uuid v1.0.3 // indirect
github.com/hashicorp/go-version v1.6.0 // indirect
github.com/hashicorp/hcl/v2 v2.16.2 // indirect
github.com/hashicorp/logutils v1.0.0 // indirect
github.com/hashicorp/terraform-plugin-go v0.14.3 // indirect
github.com/hashicorp/terraform-plugin-log v0.8.0 // indirect
github.com/hashicorp/terraform-plugin-sdk/v2 v2.26.1 // indirect
github.com/mattn/go-colorable v0.1.12 // indirect
github.com/mattn/go-isatty v0.0.14 // indirect
github.com/mitchellh/copystructure v1.2.0 // indirect
github.com/mitchellh/go-testing-interface v1.14.1 // indirect
github.com/mitchellh/go-wordwrap v1.0.0 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/mitchellh/reflectwalk v1.0.2 // indirect
github.com/vmihailenco/msgpack v4.0.4+incompatible // indirect
github.com/vmihailenco/msgpack/v4 v4.3.12 // indirect
github.com/vmihailenco/tagparser v0.1.1 // indirect
github.com/zclconf/go-cty v1.13.1 // indirect
golang.org/x/net v0.8.0 // indirect
golang.org/x/sys v0.6.0 // indirect
golang.org/x/text v0.8.0 // indirect
google.golang.org/appengine v1.6.6 // indirect
google.golang.org/protobuf v1.28.1 // indirect
)
Loading

0 comments on commit 9dd08a7

Please sign in to comment.