Skip to content

Commit 708ff9a

Browse files
authored
feat: supported onprem (#9)
1 parent 04e8ad4 commit 708ff9a

File tree

7 files changed

+55
-56
lines changed

7 files changed

+55
-56
lines changed

README.md

Lines changed: 6 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -21,20 +21,6 @@ The Timeplus provider for Terraform is a plugin that enables full lifecycle mana
2121
go install
2222
```
2323

24-
## Adding Dependencies
25-
26-
This provider uses [Go modules](https://github.com/golang/go/wiki/Modules).
27-
Please see the Go documentation for the most up to date information about using Go modules.
28-
29-
To add a new dependency `github.com/author/dependency` to your Terraform provider:
30-
31-
```shell
32-
go get github.com/author/dependency
33-
go mod tidy
34-
```
35-
36-
Then commit the changes to `go.mod` and `go.sum`.
37-
3824
## Using the provider
3925

4026
To use the provider, simply add it to your terraform file, for example:
@@ -50,10 +36,10 @@ terraform {
5036
}
5137
5238
provider "timeplus" {
53-
# the workspace ID can be found in the URL https://us.timeplus.cloud/<my-workspace-id>
54-
workspace = "my-workspace-id"
55-
# API key is required to use the provider
56-
api_key = "my-api-key"
39+
endpoint = "http://localhost:8000"
40+
workspace = "default"
41+
username = "proton"
42+
password = "proton@t+"
5743
}
5844
```
5945

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

8571
## Useful documentations for provider development
8672

87-
* Timeplus document web site: https://docs.timeplus.com/
88-
* Terraform plugin framework doc: https://developer.hashicorp.com/terraform/plugin/framework
73+
- Timeplus document web site: https://docs.timeplus.com/
74+
- Terraform plugin framework doc: https://developer.hashicorp.com/terraform/plugin/framework

docs/index.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,11 @@ provider "timeplus" {
3636

3737
### Required
3838

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

4241
### Optional
4342

44-
- `endpoint` (String) The base URL endpoint for connecting to the Timeplus workspace. When it's not set, `https://us.timeplus.cloud` will be used.
43+
- `api_key` (String, Sensitive) [Cloud] The API key to be used to call Timeplus Enterprise Cloud.
44+
- `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.
45+
- `password` (String, Sensitive) [Onprem] The password.
46+
- `username` (String) [Onprem] The username.

internal/provider/provider.go

Lines changed: 14 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,8 @@ type TimeplusProviderModel struct {
3232
Endpoint types.String `tfsdk:"endpoint"`
3333
Workspace types.String `tfsdk:"workspace"`
3434
ApiKey types.String `tfsdk:"api_key"`
35-
36-
// Ideally we should read this from stream definitions. However, there are 2 limitations
37-
// 1. Proton cluster (e.g. replica = 3) doesn't allow stream with relicatoin_refactor equals other number (e.g. 2)
38-
// 2. Proton get/list stream endpoint doesn't return relicatoin_refactor of the stream
39-
// Thus, we currently define this `replicas` as a provider setting
40-
Replicas types.Int64 `tfsdk:"replicas"`
35+
Username types.String `tfsdk:"username"`
36+
Password types.String `tfsdk:"password"`
4137
}
4238

4339
func (p *TimeplusProvider) Metadata(ctx context.Context, _ provider.MetadataRequest, resp *provider.MetadataResponse) {
@@ -52,7 +48,7 @@ func (p *TimeplusProvider) Schema(ctx context.Context, req provider.SchemaReques
5248
Use the navigation to the left to read about the available resources.`,
5349
Attributes: map[string]schema.Attribute{
5450
"endpoint": schema.StringAttribute{
55-
MarkdownDescription: "The base URL endpoint for connecting to the Timeplus workspace. When it's not set, `https://us.timeplus.cloud` will be used.",
51+
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.",
5652
Optional: true,
5753
Validators: []validator.String{myValidator.URL()},
5854
},
@@ -61,14 +57,19 @@ Use the navigation to the left to read about the available resources.`,
6157
Required: true,
6258
},
6359
"api_key": schema.StringAttribute{
64-
MarkdownDescription: "The API key to be used to call Timeplus API.",
65-
Required: true,
60+
MarkdownDescription: "[Cloud] The API key to be used to call Timeplus Enterprise Cloud.",
61+
Optional: true,
6662
Sensitive: true,
6763
},
68-
"replicas": schema.Int64Attribute{
69-
MarkdownDescription: "Number of Proton replicas",
70-
Required: false,
64+
"username": schema.StringAttribute{
65+
MarkdownDescription: "[Onprem] The username.",
66+
Optional: true,
67+
Sensitive: false,
68+
},
69+
"password": schema.StringAttribute{
70+
MarkdownDescription: "[Onprem] The password.",
7171
Optional: true,
72+
Sensitive: true,
7273
},
7374
},
7475
}
@@ -83,14 +84,8 @@ func (p *TimeplusProvider) Configure(ctx context.Context, req provider.Configure
8384
return
8485
}
8586

86-
var replicas *int
87-
if !(data.Replicas.IsNull() || data.Replicas.IsUnknown()) {
88-
valInt := int(*data.Replicas.ValueInt64Pointer())
89-
replicas = &valInt
90-
}
91-
9287
// Configuration values are now available.
93-
client, err := timeplus.NewClient(data.Workspace.ValueString(), data.ApiKey.ValueString(), replicas, timeplus.ClientOptions{
88+
client, err := timeplus.NewClient(data.Workspace.ValueString(), data.ApiKey.ValueString(), data.Username.ValueString(), data.Password.ValueString(), timeplus.ClientOptions{
9489
BaseURL: data.Endpoint.ValueString(),
9590
})
9691
if err != nil {

internal/timeplus/client.go

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,8 @@ type resource interface {
1919
type Client struct {
2020
*http.Client
2121

22-
baseURL *url.URL
23-
apiKey string
24-
replicas *int
22+
baseURL *url.URL
23+
header http.Header
2524
}
2625

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

3837
func DefaultOptions() ClientOptions {
3938
return ClientOptions{
40-
BaseURL: "https://us.timeplus.cloud",
39+
BaseURL: "https://us-west-2.timeplus.cloud",
4140
}
4241
}
4342

44-
func NewClient(workspaceID string, apiKey string, replicas *int, opts ClientOptions) (*Client, error) {
43+
func NewClient(workspaceID string, apiKey, username, password string, opts ClientOptions) (*Client, error) {
4544
ops := DefaultOptions()
4645
ops.merge(opts)
4746

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

5453
return &Client{
55-
Client: http.DefaultClient,
56-
baseURL: baseURL,
57-
apiKey: apiKey,
58-
replicas: replicas,
54+
Client: http.DefaultClient,
55+
baseURL: baseURL,
56+
header: NewHeader(apiKey, username, password),
5957
}, nil
6058
}
6159

@@ -119,7 +117,7 @@ func (c *Client) newRequest(method, url string, body io.Reader) (*http.Request,
119117
if err != nil {
120118
return nil, err
121119
}
122-
req.Header.Set("Authorization", "ApiKey "+c.apiKey)
120+
req.Header = c.header
123121
return req, nil
124122
}
125123

internal/timeplus/header.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package timeplus
2+
3+
import (
4+
"encoding/base64"
5+
"net/http"
6+
)
7+
8+
// NewHeader creates a standard Timeplus HTTP header.
9+
func NewHeader(apikey, username, password string) http.Header {
10+
header := http.Header{}
11+
12+
header.Add("Content-Type", "application/json")
13+
14+
if len(username)+len(password) > 0 {
15+
auth := username + ":" + password
16+
header.Add("Authorization", "Basic "+base64.StdEncoding.EncodeToString([]byte(auth)))
17+
} else if len(apikey) > 0 {
18+
header.Add("X-Api-Key", apikey)
19+
}
20+
21+
return header
22+
}

internal/timeplus/stream.go

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -84,10 +84,6 @@ func (Stream) resourcePath() string {
8484
}
8585

8686
func (c *Client) CreateStream(s *Stream) error {
87-
if c.replicas != nil {
88-
s.ReplicationFactor = *c.replicas
89-
}
90-
9187
if err := c.post(s); err != nil {
9288
return err
9389
}

internal/timeplus/stream_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ func newClient(t *testing.T) *timeplus.Client {
1616
} else {
1717
t.Logf("found API key: %s[=== scrubbed ===]", apiKey[:8])
1818
}
19-
c, err := timeplus.NewClient("latest", apiKey, timeplus.ClientOptions{
19+
c, err := timeplus.NewClient("latest", apiKey, "", "", timeplus.ClientOptions{
2020
BaseURL: "https://dev.timeplus.cloud",
2121
})
2222
if err != nil {

0 commit comments

Comments
 (0)