Skip to content

Commit

Permalink
feat(tokens): move condition and policy to shared models (#3610)
Browse files Browse the repository at this point in the history
  • Loading branch information
stainless-app[bot] authored and stainless-bot committed Nov 15, 2024
1 parent e322ab6 commit e5cb118
Show file tree
Hide file tree
Showing 89 changed files with 8,331 additions and 448 deletions.
95 changes: 95 additions & 0 deletions accounts/aliases.go
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 286,101 @@ const SubscriptionStateExpired = shared.SubscriptionStateExpired
// This is an alias to an internal type.
type SubscriptionParam = shared.SubscriptionParam

// This is an alias to an internal type.
type Token = shared.Token

// This is an alias to an internal type.
type TokenCondition = shared.TokenCondition

// Client IP restrictions.
//
// This is an alias to an internal type.
type TokenConditionRequestIP = shared.TokenConditionRequestIP

// Status of the token.
//
// This is an alias to an internal type.
type TokenStatus = shared.TokenStatus

// This is an alias to an internal value.
const TokenStatusActive = shared.TokenStatusActive

// This is an alias to an internal value.
const TokenStatusDisabled = shared.TokenStatusDisabled

// This is an alias to an internal value.
const TokenStatusExpired = shared.TokenStatusExpired

// This is an alias to an internal type.
type TokenParam = shared.TokenParam

// This is an alias to an internal type.
type TokenConditionParam = shared.TokenConditionParam

// Client IP restrictions.
//
// This is an alias to an internal type.
type TokenConditionRequestIPParam = shared.TokenConditionRequestIPParam

// IPv4/IPv6 CIDR.
//
// This is an alias to an internal type.
type TokenConditionCIDRList = shared.TokenConditionCIDRList

// IPv4/IPv6 CIDR.
//
// This is an alias to an internal type.
type TokenConditionCIDRListParam = shared.TokenConditionCIDRListParam

// This is an alias to an internal type.
type TokenPolicy = shared.TokenPolicy

// Allow or deny operations against the resources.
//
// This is an alias to an internal type.
type TokenPolicyEffect = shared.TokenPolicyEffect

// This is an alias to an internal value.
const TokenPolicyEffectAllow = shared.TokenPolicyEffectAllow

// This is an alias to an internal value.
const TokenPolicyEffectDeny = shared.TokenPolicyEffectDeny

// A named group of permissions that map to a group of operations against
// resources.
//
// This is an alias to an internal type.
type TokenPolicyPermissionGroup = shared.TokenPolicyPermissionGroup

// Attributes associated to the permission group.
//
// This is an alias to an internal type.
type TokenPolicyPermissionGroupsMeta = shared.TokenPolicyPermissionGroupsMeta

// A list of resource names that the policy applies to.
//
// This is an alias to an internal type.
type TokenPolicyResources = shared.TokenPolicyResources

// This is an alias to an internal type.
type TokenPolicyParam = shared.TokenPolicyParam

// A named group of permissions that map to a group of operations against
// resources.
//
// This is an alias to an internal type.
type TokenPolicyPermissionGroupParam = shared.TokenPolicyPermissionGroupParam

// Attributes associated to the permission group.
//
// This is an alias to an internal type.
type TokenPolicyPermissionGroupsMetaParam = shared.TokenPolicyPermissionGroupsMetaParam

// A list of resource names that the policy applies to.
//
// This is an alias to an internal type.
type TokenPolicyResourcesParam = shared.TokenPolicyResourcesParam

// The token value.
//
// This is an alias to an internal type.
Expand Down
29 changes: 14 additions & 15 deletions accounts/token.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 17,6 @@ import (
"github.com/cloudflare/cloudflare-go/v3/option"
"github.com/cloudflare/cloudflare-go/v3/packages/pagination"
"github.com/cloudflare/cloudflare-go/v3/shared"
"github.com/cloudflare/cloudflare-go/v3/user"
)

// TokenService contains methods and other services that help with interacting with
Expand Down Expand Up @@ -61,7 60,7 @@ func (r *TokenService) New(ctx context.Context, params TokenNewParams, opts ...o
}

// Update an existing token.
func (r *TokenService) Update(ctx context.Context, tokenID string, params TokenUpdateParams, opts ...option.RequestOption) (res *user.Token, err error) {
func (r *TokenService) Update(ctx context.Context, tokenID string, params TokenUpdateParams, opts ...option.RequestOption) (res *shared.Token, err error) {
var env TokenUpdateResponseEnvelope
opts = append(r.Options[:], opts...)
if params.AccountID.Value == "" {
Expand All @@ -82,7 81,7 @@ func (r *TokenService) Update(ctx context.Context, tokenID string, params TokenU
}

// List all Account Owned API tokens created for this account.
func (r *TokenService) List(ctx context.Context, params TokenListParams, opts ...option.RequestOption) (res *pagination.V4PagePaginationArray[user.Token], err error) {
func (r *TokenService) List(ctx context.Context, params TokenListParams, opts ...option.RequestOption) (res *pagination.V4PagePaginationArray[shared.Token], err error) {
var raw *http.Response
opts = append(r.Options[:], opts...)
opts = append([]option.RequestOption{option.WithResponseInto(&raw)}, opts...)
Expand All @@ -104,7 103,7 @@ func (r *TokenService) List(ctx context.Context, params TokenListParams, opts ..
}

// List all Account Owned API tokens created for this account.
func (r *TokenService) ListAutoPaging(ctx context.Context, params TokenListParams, opts ...option.RequestOption) *pagination.V4PagePaginationArrayAutoPager[user.Token] {
func (r *TokenService) ListAutoPaging(ctx context.Context, params TokenListParams, opts ...option.RequestOption) *pagination.V4PagePaginationArrayAutoPager[shared.Token] {
return pagination.NewV4PagePaginationArrayAutoPager(r.List(ctx, params, opts...))
}

Expand All @@ -130,7 129,7 @@ func (r *TokenService) Delete(ctx context.Context, tokenID string, body TokenDel
}

// Get information about a specific Account Owned API token.
func (r *TokenService) Get(ctx context.Context, tokenID string, query TokenGetParams, opts ...option.RequestOption) (res *user.Token, err error) {
func (r *TokenService) Get(ctx context.Context, tokenID string, query TokenGetParams, opts ...option.RequestOption) (res *shared.Token, err error) {
var env TokenGetResponseEnvelope
opts = append(r.Options[:], opts...)
if query.AccountID.Value == "" {
Expand Down Expand Up @@ -185,7 184,7 @@ type TokenNewResponse struct {
// The time before which the token MUST NOT be accepted for processing.
NotBefore time.Time `json:"not_before" format:"date-time"`
// List of access policies assigned to the token.
Policies []user.Policy `json:"policies"`
Policies []shared.TokenPolicy `json:"policies"`
// Status of the token.
Status TokenNewResponseStatus `json:"status"`
// The token value.
Expand Down Expand Up @@ -244,9 243,9 @@ func (r tokenNewResponseConditionJSON) RawJSON() string {
// Client IP restrictions.
type TokenNewResponseConditionRequestIP struct {
// List of IPv4/IPv6 CIDR addresses.
In []user.CIDRList `json:"in"`
In []shared.TokenConditionCIDRList `json:"in"`
// List of IPv4/IPv6 CIDR addresses.
NotIn []user.CIDRList `json:"not_in"`
NotIn []shared.TokenConditionCIDRList `json:"not_in"`
JSON tokenNewResponseConditionRequestIPJSON `json:"-"`
}

Expand Down Expand Up @@ -361,8 360,8 @@ type TokenNewParams struct {
// Token name.
Name param.Field[string] `json:"name,required"`
// List of access policies assigned to the token.
Policies param.Field[[]user.PolicyParam] `json:"policies,required"`
Condition param.Field[TokenNewParamsCondition] `json:"condition"`
Policies param.Field[[]shared.TokenPolicyParam] `json:"policies,required"`
Condition param.Field[TokenNewParamsCondition] `json:"condition"`
// The expiration time on or after which the JWT MUST NOT be accepted for
// processing.
ExpiresOn param.Field[time.Time] `json:"expires_on" format:"date-time"`
Expand All @@ -386,9 385,9 @@ func (r TokenNewParamsCondition) MarshalJSON() (data []byte, err error) {
// Client IP restrictions.
type TokenNewParamsConditionRequestIP struct {
// List of IPv4/IPv6 CIDR addresses.
In param.Field[[]user.CIDRListParam] `json:"in"`
In param.Field[[]shared.TokenConditionCIDRListParam] `json:"in"`
// List of IPv4/IPv6 CIDR addresses.
NotIn param.Field[[]user.CIDRListParam] `json:"not_in"`
NotIn param.Field[[]shared.TokenConditionCIDRListParam] `json:"not_in"`
}

func (r TokenNewParamsConditionRequestIP) MarshalJSON() (data []byte, err error) {
Expand Down Expand Up @@ -441,7 440,7 @@ func (r TokenNewResponseEnvelopeSuccess) IsKnown() bool {
type TokenUpdateParams struct {
// Account identifier tag.
AccountID param.Field[string] `path:"account_id,required"`
Token user.TokenParam `json:"token,required"`
Token shared.TokenParam `json:"token,required"`
}

func (r TokenUpdateParams) MarshalJSON() (data []byte, err error) {
Expand All @@ -453,7 452,7 @@ type TokenUpdateResponseEnvelope struct {
Messages []shared.ResponseInfo `json:"messages,required"`
// Whether the API call was successful
Success TokenUpdateResponseEnvelopeSuccess `json:"success,required"`
Result user.Token `json:"result"`
Result shared.Token `json:"result"`
JSON tokenUpdateResponseEnvelopeJSON `json:"-"`
}

Expand Down Expand Up @@ -584,7 583,7 @@ type TokenGetResponseEnvelope struct {
Messages []shared.ResponseInfo `json:"messages,required"`
// Whether the API call was successful
Success TokenGetResponseEnvelopeSuccess `json:"success,required"`
Result user.Token `json:"result"`
Result shared.Token `json:"result"`
JSON tokenGetResponseEnvelopeJSON `json:"-"`
}

Expand Down
82 changes: 41 additions & 41 deletions accounts/token_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 13,7 @@ import (
"github.com/cloudflare/cloudflare-go/v3/accounts"
"github.com/cloudflare/cloudflare-go/v3/internal/testutil"
"github.com/cloudflare/cloudflare-go/v3/option"
"github.com/cloudflare/cloudflare-go/v3/user"
"github.com/cloudflare/cloudflare-go/v3/shared"
)

func TestTokenNewWithOptionalParams(t *testing.T) {
Expand All @@ -32,62 32,62 @@ func TestTokenNewWithOptionalParams(t *testing.T) {
_, err := client.Accounts.Tokens.New(context.TODO(), accounts.TokenNewParams{
AccountID: cloudflare.F("eb78d65290b24279ba6f44721b3ea3c4"),
Name: cloudflare.F("readonly token"),
Policies: cloudflare.F([]user.PolicyParam{{
Effect: cloudflare.F(user.PolicyEffectAllow),
PermissionGroups: cloudflare.F([]user.PolicyPermissionGroupParam{{
Meta: cloudflare.F(user.PolicyPermissionGroupsMetaParam{
Policies: cloudflare.F([]shared.TokenPolicyParam{{
Effect: cloudflare.F(shared.TokenPolicyEffectAllow),
PermissionGroups: cloudflare.F([]shared.TokenPolicyPermissionGroupParam{{
Meta: cloudflare.F(shared.TokenPolicyPermissionGroupsMetaParam{
Key: cloudflare.F("key"),
Value: cloudflare.F("value"),
}),
}, {
Meta: cloudflare.F(user.PolicyPermissionGroupsMetaParam{
Meta: cloudflare.F(shared.TokenPolicyPermissionGroupsMetaParam{
Key: cloudflare.F("key"),
Value: cloudflare.F("value"),
}),
}}),
Resources: cloudflare.F(user.PolicyResourcesParam{
Resources: cloudflare.F(shared.TokenPolicyResourcesParam{
Resource: cloudflare.F("resource"),
Scope: cloudflare.F("scope"),
}),
}, {
Effect: cloudflare.F(user.PolicyEffectAllow),
PermissionGroups: cloudflare.F([]user.PolicyPermissionGroupParam{{
Meta: cloudflare.F(user.PolicyPermissionGroupsMetaParam{
Effect: cloudflare.F(shared.TokenPolicyEffectAllow),
PermissionGroups: cloudflare.F([]shared.TokenPolicyPermissionGroupParam{{
Meta: cloudflare.F(shared.TokenPolicyPermissionGroupsMetaParam{
Key: cloudflare.F("key"),
Value: cloudflare.F("value"),
}),
}, {
Meta: cloudflare.F(user.PolicyPermissionGroupsMetaParam{
Meta: cloudflare.F(shared.TokenPolicyPermissionGroupsMetaParam{
Key: cloudflare.F("key"),
Value: cloudflare.F("value"),
}),
}}),
Resources: cloudflare.F(user.PolicyResourcesParam{
Resources: cloudflare.F(shared.TokenPolicyResourcesParam{
Resource: cloudflare.F("resource"),
Scope: cloudflare.F("scope"),
}),
}, {
Effect: cloudflare.F(user.PolicyEffectAllow),
PermissionGroups: cloudflare.F([]user.PolicyPermissionGroupParam{{
Meta: cloudflare.F(user.PolicyPermissionGroupsMetaParam{
Effect: cloudflare.F(shared.TokenPolicyEffectAllow),
PermissionGroups: cloudflare.F([]shared.TokenPolicyPermissionGroupParam{{
Meta: cloudflare.F(shared.TokenPolicyPermissionGroupsMetaParam{
Key: cloudflare.F("key"),
Value: cloudflare.F("value"),
}),
}, {
Meta: cloudflare.F(user.PolicyPermissionGroupsMetaParam{
Meta: cloudflare.F(shared.TokenPolicyPermissionGroupsMetaParam{
Key: cloudflare.F("key"),
Value: cloudflare.F("value"),
}),
}}),
Resources: cloudflare.F(user.PolicyResourcesParam{
Resources: cloudflare.F(shared.TokenPolicyResourcesParam{
Resource: cloudflare.F("resource"),
Scope: cloudflare.F("scope"),
}),
}}),
Condition: cloudflare.F(accounts.TokenNewParamsCondition{
RequestIP: cloudflare.F(accounts.TokenNewParamsConditionRequestIP{
In: cloudflare.F([]user.CIDRListParam{"123.123.123.0/24", "2606:4700::/32"}),
NotIn: cloudflare.F([]user.CIDRListParam{"123.123.123.100/24", "2606:4700:4700::/48"}),
In: cloudflare.F([]shared.TokenConditionCIDRListParam{"123.123.123.0/24", "2606:4700::/32"}),
NotIn: cloudflare.F([]shared.TokenConditionCIDRListParam{"123.123.123.100/24", "2606:4700:4700::/48"}),
}),
}),
ExpiresOn: cloudflare.F(time.Now()),
Expand Down Expand Up @@ -120,65 120,65 @@ func TestTokenUpdateWithOptionalParams(t *testing.T) {
"ed17574386854bf78a67040be0a770b0",
accounts.TokenUpdateParams{
AccountID: cloudflare.F("eb78d65290b24279ba6f44721b3ea3c4"),
Token: user.TokenParam{
Token: shared.TokenParam{
Name: cloudflare.F("readonly token"),
Policies: cloudflare.F([]user.PolicyParam{{
Effect: cloudflare.F(user.PolicyEffectAllow),
PermissionGroups: cloudflare.F([]user.PolicyPermissionGroupParam{{
Meta: cloudflare.F(user.PolicyPermissionGroupsMetaParam{
Policies: cloudflare.F([]shared.TokenPolicyParam{{
Effect: cloudflare.F(shared.TokenPolicyEffectAllow),
PermissionGroups: cloudflare.F([]shared.TokenPolicyPermissionGroupParam{{
Meta: cloudflare.F(shared.TokenPolicyPermissionGroupsMetaParam{
Key: cloudflare.F("key"),
Value: cloudflare.F("value"),
}),
}, {
Meta: cloudflare.F(user.PolicyPermissionGroupsMetaParam{
Meta: cloudflare.F(shared.TokenPolicyPermissionGroupsMetaParam{
Key: cloudflare.F("key"),
Value: cloudflare.F("value"),
}),
}}),
Resources: cloudflare.F(user.PolicyResourcesParam{
Resources: cloudflare.F(shared.TokenPolicyResourcesParam{
Resource: cloudflare.F("resource"),
Scope: cloudflare.F("scope"),
}),
}, {
Effect: cloudflare.F(user.PolicyEffectAllow),
PermissionGroups: cloudflare.F([]user.PolicyPermissionGroupParam{{
Meta: cloudflare.F(user.PolicyPermissionGroupsMetaParam{
Effect: cloudflare.F(shared.TokenPolicyEffectAllow),
PermissionGroups: cloudflare.F([]shared.TokenPolicyPermissionGroupParam{{
Meta: cloudflare.F(shared.TokenPolicyPermissionGroupsMetaParam{
Key: cloudflare.F("key"),
Value: cloudflare.F("value"),
}),
}, {
Meta: cloudflare.F(user.PolicyPermissionGroupsMetaParam{
Meta: cloudflare.F(shared.TokenPolicyPermissionGroupsMetaParam{
Key: cloudflare.F("key"),
Value: cloudflare.F("value"),
}),
}}),
Resources: cloudflare.F(user.PolicyResourcesParam{
Resources: cloudflare.F(shared.TokenPolicyResourcesParam{
Resource: cloudflare.F("resource"),
Scope: cloudflare.F("scope"),
}),
}, {
Effect: cloudflare.F(user.PolicyEffectAllow),
PermissionGroups: cloudflare.F([]user.PolicyPermissionGroupParam{{
Meta: cloudflare.F(user.PolicyPermissionGroupsMetaParam{
Effect: cloudflare.F(shared.TokenPolicyEffectAllow),
PermissionGroups: cloudflare.F([]shared.TokenPolicyPermissionGroupParam{{
Meta: cloudflare.F(shared.TokenPolicyPermissionGroupsMetaParam{
Key: cloudflare.F("key"),
Value: cloudflare.F("value"),
}),
}, {
Meta: cloudflare.F(user.PolicyPermissionGroupsMetaParam{
Meta: cloudflare.F(shared.TokenPolicyPermissionGroupsMetaParam{
Key: cloudflare.F("key"),
Value: cloudflare.F("value"),
}),
}}),
Resources: cloudflare.F(user.PolicyResourcesParam{
Resources: cloudflare.F(shared.TokenPolicyResourcesParam{
Resource: cloudflare.F("resource"),
Scope: cloudflare.F("scope"),
}),
}}),
Status: cloudflare.F(user.TokenStatusActive),
Condition: cloudflare.F(user.TokenConditionParam{
RequestIP: cloudflare.F(user.TokenConditionRequestIPParam{
In: cloudflare.F([]user.CIDRListParam{"123.123.123.0/24", "2606:4700::/32"}),
NotIn: cloudflare.F([]user.CIDRListParam{"123.123.123.100/24", "2606:4700:4700::/48"}),
Status: cloudflare.F(shared.TokenStatusActive),
Condition: cloudflare.F(shared.TokenConditionParam{
RequestIP: cloudflare.F(shared.TokenConditionRequestIPParam{
In: cloudflare.F([]shared.TokenConditionCIDRListParam{"123.123.123.0/24", "2606:4700::/32"}),
NotIn: cloudflare.F([]shared.TokenConditionCIDRListParam{"123.123.123.100/24", "2606:4700:4700::/48"}),
}),
}),
ExpiresOn: cloudflare.F(time.Now()),
Expand Down
Loading

0 comments on commit e5cb118

Please sign in to comment.