-
Notifications
You must be signed in to change notification settings - Fork 82
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #513 from linode/proj/parent-child
new: Add support for Parent/Child account switching
- Loading branch information
Showing
23 changed files
with
970 additions
and
652 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package linodego | ||
|
||
import ( | ||
"context" | ||
) | ||
|
||
// ChildAccount represents an account under the current account. | ||
// NOTE: This is an alias to prevent any future breaking changes. | ||
type ChildAccount = Account | ||
|
||
// ChildAccountToken represents a short-lived token created using | ||
// the CreateChildAccountToken(...) function. | ||
// NOTE: This is an alias to prevent any future breaking changes. | ||
type ChildAccountToken = Token | ||
|
||
// ListChildAccounts lists child accounts under the current account. | ||
func (c *Client) ListChildAccounts(ctx context.Context, opts *ListOptions) ([]ChildAccount, error) { | ||
return getPaginatedResults[ChildAccount]( | ||
ctx, | ||
c, | ||
"account/child-accounts", | ||
opts, | ||
) | ||
} | ||
|
||
// GetChildAccount gets a single child accounts under the current account. | ||
func (c *Client) GetChildAccount(ctx context.Context, euuid string) (*ChildAccount, error) { | ||
return doGETRequest[ChildAccount]( | ||
ctx, | ||
c, | ||
formatAPIPath("account/child-accounts/%s", euuid), | ||
) | ||
} | ||
|
||
// CreateChildAccountToken creates a short-lived token that can be used to | ||
// access the Linode API under a child account. | ||
// The attributes of this token are not currently configurable. | ||
func (c *Client) CreateChildAccountToken(ctx context.Context, euuid string) (*ChildAccountToken, error) { | ||
return doPOSTRequest[ChildAccountToken, any]( | ||
ctx, | ||
c, | ||
formatAPIPath("account/child-accounts/%s/token", euuid), | ||
) | ||
} |
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
Large diffs are not rendered by default.
Oops, something went wrong.
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,40 @@ | ||
//go:build parent_child | ||
|
||
package integration | ||
|
||
import ( | ||
"context" | ||
"reflect" | ||
"testing" | ||
|
||
"github.com/google/go-cmp/cmp" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
// NOTE: These fixtures are expected to be run under a parent account. | ||
func TestAccountChild_basic(t *testing.T) { | ||
client, teardown := createTestClient(t, "fixtures/TestAccountChild_basic") | ||
defer teardown() | ||
|
||
childAccounts, err := client.ListChildAccounts(context.Background(), nil) | ||
require.NoError(t, err) | ||
require.Greater( | ||
t, | ||
len(childAccounts), | ||
0, | ||
"number of child accounts should be > 0", | ||
) | ||
|
||
childAccount, err := client.GetChildAccount(context.Background(), childAccounts[0].EUUID) | ||
require.NoError(t, err) | ||
require.True( | ||
t, | ||
reflect.DeepEqual(*childAccount, childAccounts[0]), | ||
"child accounts should be equal", | ||
cmp.Diff(*childAccount, childAccounts[0]), | ||
) | ||
|
||
token, err := client.CreateChildAccountToken(context.Background(), childAccount.EUUID) | ||
require.NoError(t, err) | ||
require.Greater(t, len(token.Token), 0) | ||
} |
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
Oops, something went wrong.