-
Notifications
You must be signed in to change notification settings - Fork 15
feat: add Cloud dedicated management api #87
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
bednar
merged 16 commits into
InfluxCommunity:main
from
jamesbalcombe83:cloud-dedicated-management-api
Jun 28, 2024
Merged
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
3948257
feat: add cloud dedicated database creation support
jamesbalcombe83 2e0e594
feat: add cloud dedicated database creation support
jamesbalcombe83 bfb6cb8
fix: improved struct names, interface functions private, remove debug…
jamesbalcombe83 299e154
fix: catch missing managementAPIURL
jamesbalcombe83 7c057c2
fix: check for nil config.ManagementAPIURL
jamesbalcombe83 2d372f7
fix: test for missing managementAPIURL
jamesbalcombe83 2ded151
fix: attempt to resolve CI error
jamesbalcombe83 433b261
fix: example function name
jamesbalcombe83 4a6b431
fix: const names to go standard format
jamesbalcombe83 e230fb7
fix: make file names more descriptive
jamesbalcombe83 18ecdb1
chore: update struct names to match schema https://github.com/influxd…
jamesbalcombe83 a4f3c98
chore: add reference to documentation
jamesbalcombe83 b5c3a38
chore: add comment to clarify default for ManagementAPIURL
jamesbalcombe83 bde221f
chore: add test for Auth token
jamesbalcombe83 38c8858
docs: Update CHANGELOG.md
bednar 80fb9eb
chore: add valid database structure
jamesbalcombe83 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| /* | ||
| The MIT License | ||
|
|
||
| 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. | ||
| */ | ||
|
|
||
| package influxdb3 | ||
|
|
||
| import ( | ||
| "context" | ||
| "log" | ||
| "net/url" | ||
| "os" | ||
| ) | ||
|
|
||
| func ExampleCloudDedicatedClient_CreateDatabase() { | ||
| managementToken := os.Getenv("INFLUX_MANAGEMENT_TOKEN") | ||
| accountID := os.Getenv("INFLUX_ACCOUNT_ID") | ||
| clusterID := os.Getenv("INFLUX_CLUSTER_ID") | ||
| managementAPIURL, err := url.Parse(os.Getenv("INFLUX_MANAGEMENT_API_URL")) | ||
| if err != nil { | ||
| log.Fatal(err) | ||
| } | ||
|
|
||
| client, err := NewFromEnv() | ||
| if err != nil { | ||
| panic(err) | ||
| } | ||
|
|
||
| cloudDedicatedConfig := CloudDedicatedClientConfig{ | ||
| AccountID: accountID, | ||
| ClusterID: clusterID, | ||
| ManagementToken: managementToken, | ||
| ManagementAPIURL: managementAPIURL, | ||
| } | ||
|
|
||
| defer client.Close() | ||
|
|
||
| cloudDedicatedClient := NewCloudDedicatedClient(client) | ||
| db := &Database{ | ||
| ClusterDatabaseName: "testDB", | ||
| ClusterDatabaseMaxTables: 500, | ||
| ClusterDatabaseMaxColumnsPerTable: 250, | ||
| ClusterDatabaseRetentionPeriod: 0, | ||
| ClusterDatabasePartitionTemplate: []PartitionTemplate{}, | ||
| } | ||
|
|
||
| if err := cloudDedicatedClient.CreateDatabase(context.Background(), &cloudDedicatedConfig, db); err != nil { | ||
| log.Fatal(err) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,148 @@ | ||
| /* | ||
| The MIT License | ||
|
|
||
| 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. | ||
| */ | ||
|
|
||
| package influxdb3 | ||
|
|
||
| import ( | ||
| "bytes" | ||
| "context" | ||
| "encoding/json" | ||
| "errors" | ||
| "fmt" | ||
| "net/http" | ||
| "net/url" | ||
| ) | ||
|
|
||
| type ( | ||
| // CloudDedicatedClient represents a client for InfluxDB Cloud Dedicated administration operations. | ||
bednar marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| // https://docs.influxdata.com/influxdb/cloud-dedicated/admin/databases/create/?t=Management+API | ||
| CloudDedicatedClient struct { | ||
| client *Client | ||
| } | ||
|
|
||
| CloudDedicatedClientConfig struct { | ||
| AccountID string | ||
| ClusterID string | ||
| ManagementToken string | ||
| ManagementAPIURL *url.URL // default is https://console.influxdata.com | ||
| } | ||
|
|
||
| Database struct { | ||
| ClusterDatabaseName string `json:"name"` | ||
| ClusterDatabaseMaxTables uint64 `json:"maxTables"` // default 500 | ||
| ClusterDatabaseMaxColumnsPerTable uint64 `json:"maxColumnsPerTable"` // default 250 | ||
| ClusterDatabaseRetentionPeriod uint64 `json:"retentionPeriod"` // nanoseconds default 0 is infinite | ||
| ClusterDatabasePartitionTemplate []PartitionTemplate `json:"partitionTemplate"` // Tag or TagBucket, limit is total of 7 | ||
| } | ||
|
|
||
| PartitionTemplate interface { | ||
| isPartitionTemplate() | ||
| } | ||
|
|
||
| Tag struct { | ||
| Type string `json:"type"` | ||
| Value string `json:"value"` | ||
| } | ||
|
|
||
| TagBucket struct { | ||
| Type string `json:"type"` | ||
| Value TagBucketValue `json:"value"` | ||
| } | ||
|
|
||
| TagBucketValue struct { | ||
| TagName string `json:"tagName"` | ||
| NumberOfBuckets uint64 `json:"numberOfBuckets"` | ||
| } | ||
| ) | ||
|
|
||
| var ( | ||
| MaxPartitions = 7 | ||
| ManagementAPIURL = "https://console.influxdata.com" | ||
| ) | ||
|
|
||
| func (t Tag) isPartitionTemplate() {} | ||
| func (tb TagBucket) isPartitionTemplate() {} | ||
|
|
||
| // NewCloudDedicatedClient creates new CloudDedicatedClient with given InfluxDB client. | ||
| func NewCloudDedicatedClient(client *Client) *CloudDedicatedClient { | ||
| return &CloudDedicatedClient{client: client} | ||
| } | ||
|
|
||
| // CreateDatabase creates a new database | ||
| func (d *CloudDedicatedClient) CreateDatabase(ctx context.Context, config *CloudDedicatedClientConfig, db *Database) error { | ||
| if db == nil { | ||
| return errors.New("database must not nil") | ||
| } | ||
|
|
||
| if d.client.config.Database == "" { | ||
| return errors.New("database name must not be empty") | ||
| } | ||
| db.ClusterDatabaseName = d.client.config.Database | ||
|
|
||
| if len(db.ClusterDatabasePartitionTemplate) > MaxPartitions { | ||
| return fmt.Errorf("partition template should not have more than %d tags or tag buckets", MaxPartitions) | ||
| } | ||
|
|
||
| if db.ClusterDatabaseMaxTables == 0 { | ||
| db.ClusterDatabaseMaxTables = uint64(500) | ||
| } | ||
|
|
||
| if db.ClusterDatabaseMaxColumnsPerTable == 0 { | ||
| db.ClusterDatabaseMaxColumnsPerTable = uint64(250) | ||
| } | ||
|
|
||
| path := fmt.Sprintf("/api/v0/accounts/%s/clusters/%s/databases", config.AccountID, config.ClusterID) | ||
|
|
||
| return d.createDatabase(ctx, path, db, config) | ||
| } | ||
|
|
||
| // createDatabase is a helper function for CreateDatabase to enhance test coverage. | ||
| func (d *CloudDedicatedClient) createDatabase(ctx context.Context, path string, db any, config *CloudDedicatedClientConfig) error { | ||
| if config.ManagementAPIURL == nil { | ||
| config.ManagementAPIURL, _ = url.Parse(ManagementAPIURL) | ||
| } | ||
| u, err := config.ManagementAPIURL.Parse(path) | ||
| if err != nil { | ||
| return fmt.Errorf("failed to parse database creation path: %w", err) | ||
| } | ||
|
|
||
| body, err := json.Marshal(db) | ||
| if err != nil { | ||
| return fmt.Errorf("failed to marshal database creation request body: %w", err) | ||
| } | ||
|
|
||
| headers := http.Header{} | ||
| headers.Set("Content-Type", "application/json") | ||
| headers.Set("Accept", "application/json") | ||
| headers.Set("Authorization", "Bearer "+config.ManagementToken) | ||
|
|
||
| param := httpParams{ | ||
| endpointURL: u, | ||
| queryParams: nil, | ||
| httpMethod: "POST", | ||
| headers: headers, | ||
| body: bytes.NewReader(body), | ||
| } | ||
|
|
||
| _, err = d.client.makeAPICall(ctx, param) | ||
| return err | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.