Skip to content

Commit

Permalink
New resource: split_api_key (#91)
Browse files Browse the repository at this point in the history
* Implement new resource: `split_api_key`

* update go.sum

* update ci ymls
  • Loading branch information
davidji99 authored Mar 2, 2024
1 parent 114b758 commit fe39161
Show file tree
Hide file tree
Showing 15 changed files with 577 additions and 9 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/documentation.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ on:
- docs/**

env:
GO_VERSION: "1.18"
GO_VERSION: "1.21"
GO111MODULE: on

jobs:
Expand Down
8 changes: 4 additions & 4 deletions .github/workflows/golangci-lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@ jobs:
steps:
- uses: actions/setup-go@v5
with:
go-version: 1.18
- uses: actions/checkout@v4.1.1
go-version: 1.21
- uses: actions/checkout@v4
- name: golangci-lint
uses: golangci/golangci-lint-action@v4.0.0
uses: golangci/golangci-lint-action@v4
with:
# Required: the version of golangci-lint is required and must be specified without patch version:
# we always use the latest patch version.
version: v1.50.1
version: v1.55.2

# Optional: working directory, useful for monorepos
# working-directory: somedir
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ jobs:
name: Set up Go
uses: actions/setup-go@v5
with:
go-version: 1.20.7
go-version: 1.21
-
name: Import GPG key
id: import_gpg
Expand All @@ -28,7 +28,7 @@ jobs:
name: Run GoReleaser
uses: goreleaser/[email protected]
with:
version: v1.20
version: v1.21
args: release --rm-dist
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ jobs:
test:
strategy:
matrix:
go-version: [1.20.x]
go-version: [1.21.x, 1.22.x]
platform: [ubuntu-latest]
runs-on: ${{ matrix.platform }}

Expand Down
81 changes: 81 additions & 0 deletions api/api-accessors.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ type Client struct {
expiresAt time.Time

// Services used for talking to different parts of the Sendgrid APIv3.
ApiKeys *KeysService
Attributes *AttributesService
Environments *EnvironmentsService
Groups *GroupsService
Expand Down Expand Up @@ -115,6 +116,7 @@ func New(opts ...Option) (*Client, error) {
// injectServices adds the services to the client.
func (c *Client) injectServices() {
c.common.client = c
c.ApiKeys = (*KeysService)(&c.common)
c.Attributes = (*AttributesService)(&c.common)
c.Environments = (*EnvironmentsService)(&c.common)
c.Groups = (*GroupsService)(&c.common)
Expand Down
72 changes: 72 additions & 0 deletions api/api_keys.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package api

import "github.com/davidji99/simpleresty"

var (
ValidApiKeyTypes = []string{"client_side", "server_side", "admin"}
ValidApiKeyRoles = []string{"API_ALL_GRANTED", "API_APIKEY", "API_ADMIN", "API_WORKSPACE_ADMIN", "API_FEATURE_FLAG_VIEWER",
"API_FEATURE_FLAG_EDITOR", "API_SEGMENT_VIEWER", "API_SEGMENT_EDITOR"}
)

// KeysService handles communication with the api keys related
// methods of the Split.io APIv2.
//
// Reference: https://docs.split.io/reference/api-keys-overview
type KeysService service

// KeyRequest represents a request to create an API key.
type KeyRequest struct {
Name string `json:"name"`
KeyType string `json:"apiKeyType"`
Roles []string `json:"roles"`
Environments []KeyEnvironmentRequest `json:"environments"`
Workspace *KeyWorkspaceRequest `json:"workspace"`
}

type KeyEnvironmentRequest struct {
Type string `json:"type"`
Id string `json:"id"`
}

type KeyWorkspaceRequest struct {
Type string `json:"type"`
Id string `json:"id"`
}

// KeyResponse represents the created key.
//
// Not all fields are added here.
type KeyResponse struct {
Id *string `json:"id"`
Name *string `json:"name"`
Roles []string `json:"roles"`
Type *string `json:"type"`
ApiKeyType *string `json:"apiKeyType"`

// Key is the actual API key
Key *string `json:"key"`
}

// Create an API key.
//
// Reference: https://docs.split.io/reference/create-an-api-key
func (k *KeysService) Create(opts *KeyRequest) (*KeyResponse, *simpleresty.Response, error) {
var result KeyResponse
urlStr := k.client.http.RequestURL("/apiKeys")

// Execute the request
response, createErr := k.client.post(urlStr, &result, opts)

return &result, response, createErr
}

// Delete an API key.
//
// Reference: https://docs.split.io/reference/delete-an-api-key
func (k *KeysService) Delete(key string) (*simpleresty.Response, error) {
urlStr := k.client.http.RequestURL("/apiKeys/%s", key)
// Execute the request
response, err := k.client.delete(urlStr, nil, nil)

return response, err
}
61 changes: 61 additions & 0 deletions docs/resources/api_key.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
---
layout: "split"
page_title: "Split: split_api_key"
sidebar_current: "docs-split-resource-api-key"
description: |-
Provides the ability to manage a Split API key.
---

# split_api_key

This resource provides the ability to manage an [API key](https://docs.split.io/reference/api-keys-overview).

Due to API limitations, it is not possible update an existing API key. Any modifications to a `split_api_key` resource
will result in a destroy and recreate process.

-> **IMPORTANT!**
Please be very careful when deleting this resource as the deleted API keys are NOT recoverable and invalidated immediately.
Furthermore, this resource renders the actual API key plain-text in your state file.
Please ensure that your state file is properly secured and encrypted at rest.

## Example Usage

```hcl-terraform
data "split_workspace" "default" {
name = "default"
}
resource "split_environment" "foobar" {
workspace_id = data.split_workspace.default.id
name = "staging"
production = true
}
resource "split_api_key" "foobar" {
workspace_id = data.split_workspace.default.id
name = "my client side key"
type = "client_side"
environment_ids = [split_environment.foobar.id]
}
```

## Argument Reference

The following arguments are supported:

* `workspace_id` - (Required) `<string>` The UUID of the workspace you want to create the environment in.
* `environment_ids` - (Required) `<list(string)>` List of environment UUIDs.
* `name` - (Required) `<boolean>` Name of the API key.
* `type` - (Required) `<boolean>` Type of the API key. Refer to Split [documentation](https://docs.split.io/reference/create-an-api-key#supported-types) on acceptable values, case sensitive.
* `roles` - (Required) `<boolean>` Supported only when `type=admin` API keys. For the full list of allowed Admin API Key roles, refer
to Split [documentation](https://docs.split.io/reference/api-keys-overview#admin-api-key-roles), case sensitive.

## Attributes Reference

The following attributes are exported:

n/a

## Import

Due to Split API limitations, it is not possible to import an existing API key.
4 changes: 3 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
module github.com/davidji99/terraform-provider-split

go 1.20
go 1.21

toolchain go1.22.0

require (
github.com/davidji99/simpleresty v0.4.2
Expand Down
Loading

0 comments on commit fe39161

Please sign in to comment.