Skip to content

Commit

Permalink
feat: supported onprem (#9)
Browse files Browse the repository at this point in the history
  • Loading branch information
ye11ow authored Dec 23, 2024
1 parent 04e8ad4 commit 708ff9a
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 56 deletions.
26 changes: 6 additions & 20 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,6 @@ The Timeplus provider for Terraform is a plugin that enables full lifecycle mana
go install
```

## Adding Dependencies

This provider uses [Go modules](https://github.com/golang/go/wiki/Modules).
Please see the Go documentation for the most up to date information about using Go modules.

To add a new dependency `github.com/author/dependency` to your Terraform provider:

```shell
go get github.com/author/dependency
go mod tidy
```

Then commit the changes to `go.mod` and `go.sum`.

## Using the provider

To use the provider, simply add it to your terraform file, for example:
Expand All @@ -50,10 +36,10 @@ terraform {
}
provider "timeplus" {
# the workspace ID can be found in the URL https://us.timeplus.cloud/<my-workspace-id>
workspace = "my-workspace-id"
# API key is required to use the provider
api_key = "my-api-key"
endpoint = "http://localhost:8000"
workspace = "default"
username = "proton"
password = "proton@t+"
}
```

Expand Down Expand Up @@ -84,5 +70,5 @@ To generate or update documentation, run `go generate`.

## Useful documentations for provider development

* Timeplus document web site: https://docs.timeplus.com/
* Terraform plugin framework doc: https://developer.hashicorp.com/terraform/plugin/framework
- Timeplus document web site: https://docs.timeplus.com/
- Terraform plugin framework doc: https://developer.hashicorp.com/terraform/plugin/framework
6 changes: 4 additions & 2 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,11 @@ provider "timeplus" {

### Required

- `api_key` (String, Sensitive) The API key to be used to call Timeplus API.
- `workspace` (String) The ID of the workspace in which the provider manages resources.

### Optional

- `endpoint` (String) The base URL endpoint for connecting to the Timeplus workspace. When it's not set, `https://us.timeplus.cloud` will be used.
- `api_key` (String, Sensitive) [Cloud] The API key to be used to call Timeplus Enterprise Cloud.
- `endpoint` (String) The base URL endpoint for connecting to the Timeplus workspace. When it's not set, `https://us-west-2.timeplus.cloud` will be used.
- `password` (String, Sensitive) [Onprem] The password.
- `username` (String) [Onprem] The username.
33 changes: 14 additions & 19 deletions internal/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,8 @@ type TimeplusProviderModel struct {
Endpoint types.String `tfsdk:"endpoint"`
Workspace types.String `tfsdk:"workspace"`
ApiKey types.String `tfsdk:"api_key"`

// Ideally we should read this from stream definitions. However, there are 2 limitations
// 1. Proton cluster (e.g. replica = 3) doesn't allow stream with relicatoin_refactor equals other number (e.g. 2)
// 2. Proton get/list stream endpoint doesn't return relicatoin_refactor of the stream
// Thus, we currently define this `replicas` as a provider setting
Replicas types.Int64 `tfsdk:"replicas"`
Username types.String `tfsdk:"username"`
Password types.String `tfsdk:"password"`
}

func (p *TimeplusProvider) Metadata(ctx context.Context, _ provider.MetadataRequest, resp *provider.MetadataResponse) {
Expand All @@ -52,7 +48,7 @@ func (p *TimeplusProvider) Schema(ctx context.Context, req provider.SchemaReques
Use the navigation to the left to read about the available resources.`,
Attributes: map[string]schema.Attribute{
"endpoint": schema.StringAttribute{
MarkdownDescription: "The base URL endpoint for connecting to the Timeplus workspace. When it's not set, `https://us.timeplus.cloud` will be used.",
MarkdownDescription: "The base URL endpoint for connecting to the Timeplus workspace. When it's not set, `https://us-west-2.timeplus.cloud` will be used.",
Optional: true,
Validators: []validator.String{myValidator.URL()},
},
Expand All @@ -61,14 +57,19 @@ Use the navigation to the left to read about the available resources.`,
Required: true,
},
"api_key": schema.StringAttribute{
MarkdownDescription: "The API key to be used to call Timeplus API.",
Required: true,
MarkdownDescription: "[Cloud] The API key to be used to call Timeplus Enterprise Cloud.",
Optional: true,
Sensitive: true,
},
"replicas": schema.Int64Attribute{
MarkdownDescription: "Number of Proton replicas",
Required: false,
"username": schema.StringAttribute{
MarkdownDescription: "[Onprem] The username.",
Optional: true,
Sensitive: false,
},
"password": schema.StringAttribute{
MarkdownDescription: "[Onprem] The password.",
Optional: true,
Sensitive: true,
},
},
}
Expand All @@ -83,14 +84,8 @@ func (p *TimeplusProvider) Configure(ctx context.Context, req provider.Configure
return
}

var replicas *int
if !(data.Replicas.IsNull() || data.Replicas.IsUnknown()) {
valInt := int(*data.Replicas.ValueInt64Pointer())
replicas = &valInt
}

// Configuration values are now available.
client, err := timeplus.NewClient(data.Workspace.ValueString(), data.ApiKey.ValueString(), replicas, timeplus.ClientOptions{
client, err := timeplus.NewClient(data.Workspace.ValueString(), data.ApiKey.ValueString(), data.Username.ValueString(), data.Password.ValueString(), timeplus.ClientOptions{
BaseURL: data.Endpoint.ValueString(),
})
if err != nil {
Expand Down
18 changes: 8 additions & 10 deletions internal/timeplus/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,8 @@ type resource interface {
type Client struct {
*http.Client

baseURL *url.URL
apiKey string
replicas *int
baseURL *url.URL
header http.Header
}

// optional configurations for the client
Expand All @@ -37,11 +36,11 @@ func (o *ClientOptions) merge(other ClientOptions) {

func DefaultOptions() ClientOptions {
return ClientOptions{
BaseURL: "https://us.timeplus.cloud",
BaseURL: "https://us-west-2.timeplus.cloud",
}
}

func NewClient(workspaceID string, apiKey string, replicas *int, opts ClientOptions) (*Client, error) {
func NewClient(workspaceID string, apiKey, username, password string, opts ClientOptions) (*Client, error) {
ops := DefaultOptions()
ops.merge(opts)

Expand All @@ -52,10 +51,9 @@ func NewClient(workspaceID string, apiKey string, replicas *int, opts ClientOpti
baseURL = baseURL.JoinPath(workspaceID, "api", "v1beta2")

return &Client{
Client: http.DefaultClient,
baseURL: baseURL,
apiKey: apiKey,
replicas: replicas,
Client: http.DefaultClient,
baseURL: baseURL,
header: NewHeader(apiKey, username, password),
}, nil
}

Expand Down Expand Up @@ -119,7 +117,7 @@ func (c *Client) newRequest(method, url string, body io.Reader) (*http.Request,
if err != nil {
return nil, err
}
req.Header.Set("Authorization", "ApiKey "+c.apiKey)
req.Header = c.header
return req, nil
}

Expand Down
22 changes: 22 additions & 0 deletions internal/timeplus/header.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package timeplus

import (
"encoding/base64"
"net/http"
)

// NewHeader creates a standard Timeplus HTTP header.
func NewHeader(apikey, username, password string) http.Header {
header := http.Header{}

header.Add("Content-Type", "application/json")

if len(username)+len(password) > 0 {
auth := username + ":" + password
header.Add("Authorization", "Basic "+base64.StdEncoding.EncodeToString([]byte(auth)))
} else if len(apikey) > 0 {
header.Add("X-Api-Key", apikey)
}

return header
}
4 changes: 0 additions & 4 deletions internal/timeplus/stream.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,6 @@ func (Stream) resourcePath() string {
}

func (c *Client) CreateStream(s *Stream) error {
if c.replicas != nil {
s.ReplicationFactor = *c.replicas
}

if err := c.post(s); err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion internal/timeplus/stream_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ func newClient(t *testing.T) *timeplus.Client {
} else {
t.Logf("found API key: %s[=== scrubbed ===]", apiKey[:8])
}
c, err := timeplus.NewClient("latest", apiKey, timeplus.ClientOptions{
c, err := timeplus.NewClient("latest", apiKey, "", "", timeplus.ClientOptions{
BaseURL: "https://dev.timeplus.cloud",
})
if err != nil {
Expand Down

0 comments on commit 708ff9a

Please sign in to comment.