-
Notifications
You must be signed in to change notification settings - Fork 24
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
feat: key management support #496
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
9bf24f0
feat: key management support
jharley d06404d
Add test case for half-configured v2 config
jharley ce17d27
move error handling back to client package
jharley a11c290
tidy up error refactor
jharley 18cc226
address PR feedback
jharley 6761e5e
typo
jharley 22c2e50
add jsonapi errpayload test
jharley 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 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 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 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 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 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,115 @@ | ||
package v2 | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"net/http" | ||
|
||
"github.com/hashicorp/jsonapi" | ||
|
||
hnyclient "github.com/honeycombio/terraform-provider-honeycombio/client" | ||
) | ||
|
||
// Compile-time proof of interface implementation. | ||
var _ APIKeys = (*apiKeys)(nil) | ||
|
||
type APIKeys interface { | ||
Create(ctx context.Context, key *APIKey) (*APIKey, error) | ||
Get(ctx context.Context, id string) (*APIKey, error) | ||
Update(ctx context.Context, key *APIKey) (*APIKey, error) | ||
Delete(ctx context.Context, id string) error | ||
List(ctx context.Context, opts ...ListOption) (*Pager[APIKey], error) | ||
} | ||
|
||
const ( | ||
apiKeysPath = "/2/teams/%s/api-keys" | ||
apiKeysByIDPath = "/2/teams/%s/api-keys/%s" | ||
) | ||
|
||
type apiKeys struct { | ||
client *Client | ||
authinfo *AuthMetadata | ||
} | ||
|
||
func (a *apiKeys) Create(ctx context.Context, k *APIKey) (*APIKey, error) { | ||
r, err := a.client.Do(ctx, | ||
http.MethodPost, | ||
fmt.Sprintf(apiKeysPath, a.authinfo.Team.Slug), | ||
k, | ||
) | ||
if err != nil { | ||
return nil, err | ||
} | ||
if r.StatusCode != http.StatusCreated { | ||
return nil, hnyclient.ErrorFromResponse(r) | ||
} | ||
|
||
key := new(APIKey) | ||
if err := jsonapi.UnmarshalPayload(r.Body, key); err != nil { | ||
return nil, err | ||
} | ||
return key, nil | ||
} | ||
|
||
func (a *apiKeys) Get(ctx context.Context, id string) (*APIKey, error) { | ||
r, err := a.client.Do(ctx, | ||
http.MethodGet, | ||
fmt.Sprintf(apiKeysByIDPath, a.authinfo.Team.Slug, id), | ||
nil, | ||
) | ||
if err != nil { | ||
return nil, err | ||
} | ||
if r.StatusCode != http.StatusOK { | ||
return nil, hnyclient.ErrorFromResponse(r) | ||
} | ||
|
||
key := new(APIKey) | ||
if err := jsonapi.UnmarshalPayload(r.Body, key); err != nil { | ||
return nil, err | ||
} | ||
return key, nil | ||
} | ||
|
||
func (a *apiKeys) Update(ctx context.Context, k *APIKey) (*APIKey, error) { | ||
r, err := a.client.Do(ctx, | ||
http.MethodPatch, | ||
fmt.Sprintf(apiKeysByIDPath, a.authinfo.Team.Slug, k.ID), | ||
k, | ||
) | ||
if err != nil { | ||
return nil, err | ||
} | ||
if r.StatusCode != http.StatusOK { | ||
return nil, hnyclient.ErrorFromResponse(r) | ||
} | ||
|
||
key := new(APIKey) | ||
if err := jsonapi.UnmarshalPayload(r.Body, key); err != nil { | ||
return nil, err | ||
} | ||
return key, nil | ||
} | ||
|
||
func (a *apiKeys) Delete(ctx context.Context, id string) error { | ||
r, err := a.client.Do(ctx, | ||
http.MethodDelete, | ||
fmt.Sprintf(apiKeysByIDPath, a.authinfo.Team.Slug, id), | ||
nil, | ||
) | ||
if err != nil { | ||
return err | ||
} | ||
if r.StatusCode != http.StatusNoContent { | ||
return hnyclient.ErrorFromResponse(r) | ||
} | ||
return nil | ||
} | ||
|
||
func (a *apiKeys) List(ctx context.Context, os ...ListOption) (*Pager[APIKey], error) { | ||
return NewPager[APIKey]( | ||
a.client, | ||
fmt.Sprintf(apiKeysPath, a.authinfo.Team.Slug), | ||
os..., | ||
) | ||
} |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
will drop these (here and below) when the v2client has Environments API support