github.com/anthropics/anthropic-sdk-go@v1.70.1/betaorganizationserviceaccount.go (about)

     1  // File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
     2  
     3  package anthropic
     4  
     5  import (
     6  	"context"
     7  	"errors"
     8  	"fmt"
     9  	"net/http"
    10  	"net/url"
    11  	"slices"
    12  	"time"
    13  
    14  	"github.com/anthropics/anthropic-sdk-go/internal/apijson"
    15  	"github.com/anthropics/anthropic-sdk-go/internal/apiquery"
    16  	"github.com/anthropics/anthropic-sdk-go/internal/requestconfig"
    17  	"github.com/anthropics/anthropic-sdk-go/option"
    18  	"github.com/anthropics/anthropic-sdk-go/packages/pagination"
    19  	"github.com/anthropics/anthropic-sdk-go/packages/param"
    20  	"github.com/anthropics/anthropic-sdk-go/packages/respjson"
    21  	"github.com/anthropics/anthropic-sdk-go/shared/constant"
    22  )
    23  
    24  // BetaOrganizationServiceAccountService contains methods and other services that
    25  // help with interacting with the anthropic API.
    26  //
    27  // Note, unlike clients, this service does not read variables from the environment
    28  // automatically. You should not instantiate this service directly, and instead use
    29  // the [NewBetaOrganizationServiceAccountService] method instead.
    30  type BetaOrganizationServiceAccountService struct {
    31  	Options    []option.RequestOption
    32  	Workspaces BetaOrganizationServiceAccountWorkspaceService
    33  }
    34  
    35  // NewBetaOrganizationServiceAccountService generates a new service that applies
    36  // the given options to each request. These options are applied after the parent
    37  // client's options (if there is one), and before any request-specific options.
    38  func NewBetaOrganizationServiceAccountService(opts ...option.RequestOption) (r BetaOrganizationServiceAccountService) {
    39  	r = BetaOrganizationServiceAccountService{}
    40  	r.Options = opts
    41  	r.Workspaces = NewBetaOrganizationServiceAccountWorkspaceService(opts...)
    42  	return
    43  }
    44  
    45  // **Requires an OAuth access token with the `org:admin` scope**, from
    46  // `ant auth login --scope org:admin` or a workload identity federation rule; Admin
    47  // API keys are not accepted. See
    48  // [Manage WIF with the Admin API](/docs/en/manage-claude/wif-admin-api).
    49  //
    50  // Create a service account.
    51  //
    52  // A service account is a named workload identity that federation rules target.
    53  // `organization_role` is `developer` (default) or `admin`; a rule may only be
    54  // created or retargeted to grant `org:admin` scope when the target's
    55  // `organization_role` is `admin`. Creating an `admin`-role service account
    56  // requires an interactive credential (a user OAuth token or a Console session) — a
    57  // workload may only create `developer`-role service accounts.
    58  func (r *BetaOrganizationServiceAccountService) New(ctx context.Context, params BetaOrganizationServiceAccountNewParams, opts ...option.RequestOption) (res *BetaServiceAccount, err error) {
    59  	for _, v := range params.Betas {
    60  		opts = append(opts, option.WithHeaderAdd("anthropic-beta", fmt.Sprintf("%v", v)))
    61  	}
    62  	opts = slices.Concat(r.Options, opts)
    63  	path := "v1/organizations/service_accounts?beta=true"
    64  	err = requestconfig.ExecuteNewRequest(ctx, http.MethodPost, path, params, &res, opts...)
    65  	return res, err
    66  }
    67  
    68  // **Requires an OAuth access token with the `org:admin` scope**, from
    69  // `ant auth login --scope org:admin` or a workload identity federation rule; Admin
    70  // API keys are not accepted. See
    71  // [Manage WIF with the Admin API](/docs/en/manage-claude/wif-admin-api).
    72  //
    73  // Retrieve a service account by its ID (`svac_...`).
    74  func (r *BetaOrganizationServiceAccountService) Get(ctx context.Context, serviceAccountID string, query BetaOrganizationServiceAccountGetParams, opts ...option.RequestOption) (res *BetaServiceAccount, err error) {
    75  	for _, v := range query.Betas {
    76  		opts = append(opts, option.WithHeaderAdd("anthropic-beta", fmt.Sprintf("%v", v)))
    77  	}
    78  	opts = slices.Concat(r.Options, opts)
    79  	if serviceAccountID == "" {
    80  		err = errors.New("missing required service_account_id parameter")
    81  		return nil, err
    82  	}
    83  	path := fmt.Sprintf("v1/organizations/service_accounts/%s?beta=true", serviceAccountID)
    84  	err = requestconfig.ExecuteNewRequest(ctx, http.MethodGet, path, nil, &res, opts...)
    85  	return res, err
    86  }
    87  
    88  // **Requires an OAuth access token with the `org:admin` scope**, from
    89  // `ant auth login --scope org:admin` or a workload identity federation rule; Admin
    90  // API keys are not accepted. See
    91  // [Manage WIF with the Admin API](/docs/en/manage-claude/wif-admin-api).
    92  //
    93  // Update a service account.
    94  //
    95  // Only `description` and `organization_role` are mutable; `name` cannot be
    96  // changed. Archived service accounts cannot be updated; this returns 400. Setting
    97  // `organization_role` to `admin` (even when unchanged) requires an interactive
    98  // credential (a user OAuth token or a Console session).
    99  func (r *BetaOrganizationServiceAccountService) Update(ctx context.Context, serviceAccountID string, params BetaOrganizationServiceAccountUpdateParams, opts ...option.RequestOption) (res *BetaServiceAccount, err error) {
   100  	for _, v := range params.Betas {
   101  		opts = append(opts, option.WithHeaderAdd("anthropic-beta", fmt.Sprintf("%v", v)))
   102  	}
   103  	opts = slices.Concat(r.Options, opts)
   104  	if serviceAccountID == "" {
   105  		err = errors.New("missing required service_account_id parameter")
   106  		return nil, err
   107  	}
   108  	path := fmt.Sprintf("v1/organizations/service_accounts/%s?beta=true", serviceAccountID)
   109  	err = requestconfig.ExecuteNewRequest(ctx, http.MethodPost, path, params, &res, opts...)
   110  	return res, err
   111  }
   112  
   113  // **Requires an OAuth access token with the `org:admin` scope**, from
   114  // `ant auth login --scope org:admin` or a workload identity federation rule; Admin
   115  // API keys are not accepted. See
   116  // [Manage WIF with the Admin API](/docs/en/manage-claude/wif-admin-api).
   117  //
   118  // List service accounts in the caller's organization.
   119  //
   120  // Results are ordered by creation time, newest first. Use `limit` and the
   121  // `next_page` cursor to paginate; set `include_archived=true` to include archived
   122  // service accounts.
   123  func (r *BetaOrganizationServiceAccountService) List(ctx context.Context, params BetaOrganizationServiceAccountListParams, opts ...option.RequestOption) (res *pagination.PageCursor[BetaServiceAccount], err error) {
   124  	var raw *http.Response
   125  	for _, v := range params.Betas {
   126  		opts = append(opts, option.WithHeaderAdd("anthropic-beta", fmt.Sprintf("%v", v)))
   127  	}
   128  	opts = slices.Concat(r.Options, opts)
   129  	opts = append([]option.RequestOption{option.WithResponseInto(&raw)}, opts...)
   130  	path := "v1/organizations/service_accounts?beta=true"
   131  	cfg, err := requestconfig.NewRequestConfig(ctx, http.MethodGet, path, params, &res, opts...)
   132  	if err != nil {
   133  		return nil, err
   134  	}
   135  	err = cfg.Execute()
   136  	if err != nil {
   137  		return nil, err
   138  	}
   139  	res.SetPageConfig(cfg, raw)
   140  	return res, nil
   141  }
   142  
   143  // **Requires an OAuth access token with the `org:admin` scope**, from
   144  // `ant auth login --scope org:admin` or a workload identity federation rule; Admin
   145  // API keys are not accepted. See
   146  // [Manage WIF with the Admin API](/docs/en/manage-claude/wif-admin-api).
   147  //
   148  // List service accounts in the caller's organization.
   149  //
   150  // Results are ordered by creation time, newest first. Use `limit` and the
   151  // `next_page` cursor to paginate; set `include_archived=true` to include archived
   152  // service accounts.
   153  func (r *BetaOrganizationServiceAccountService) ListAutoPaging(ctx context.Context, params BetaOrganizationServiceAccountListParams, opts ...option.RequestOption) *pagination.PageCursorAutoPager[BetaServiceAccount] {
   154  	return pagination.NewPageCursorAutoPager(r.List(ctx, params, opts...))
   155  }
   156  
   157  // **Requires an OAuth access token with the `org:admin` scope**, from
   158  // `ant auth login --scope org:admin` or a workload identity federation rule; Admin
   159  // API keys are not accepted. See
   160  // [Manage WIF with the Admin API](/docs/en/manage-claude/wif-admin-api).
   161  //
   162  // Archive a service account.
   163  //
   164  // Idempotent; re-archiving returns the service account with its original
   165  // `archived_at`. Rejected with 400 if any live (non-archived) federation rule
   166  // still targets this service account, same as issuer archival; archive those rules
   167  // first or change their target to another service account.
   168  func (r *BetaOrganizationServiceAccountService) Archive(ctx context.Context, serviceAccountID string, body BetaOrganizationServiceAccountArchiveParams, opts ...option.RequestOption) (res *BetaServiceAccount, err error) {
   169  	for _, v := range body.Betas {
   170  		opts = append(opts, option.WithHeaderAdd("anthropic-beta", fmt.Sprintf("%v", v)))
   171  	}
   172  	opts = slices.Concat(r.Options, opts)
   173  	if serviceAccountID == "" {
   174  		err = errors.New("missing required service_account_id parameter")
   175  		return nil, err
   176  	}
   177  	path := fmt.Sprintf("v1/organizations/service_accounts/%s/archive?beta=true", serviceAccountID)
   178  	err = requestconfig.ExecuteNewRequest(ctx, http.MethodPost, path, nil, &res, opts...)
   179  	return res, err
   180  }
   181  
   182  // Named non-human identity within the caller's organization.
   183  //
   184  // A service account is a pure identity: name + org. Authorization lives on
   185  // whatever references it (federation rules).
   186  type BetaServiceAccount struct {
   187  	// Tagged ID of the service account.
   188  	ID string `json:"id" api:"required"`
   189  	// If set, this service account is archived.
   190  	ArchivedAt time.Time `json:"archived_at" api:"required" format:"date-time"`
   191  	// Tagged ID (`user_`/`svac_`) of the actor that archived this service account.
   192  	ArchivedByActorID string `json:"archived_by_actor_id" api:"required"`
   193  	// When this service account was created.
   194  	CreatedAt time.Time `json:"created_at" api:"required" format:"date-time"`
   195  	// Tagged ID (`user_`/`svac_`) of the actor that created this service account.
   196  	CreatedByActorID string `json:"created_by_actor_id" api:"required"`
   197  	// Optional free-text description.
   198  	Description string `json:"description" api:"required"`
   199  	// Admin-chosen slug identifier.
   200  	Name string `json:"name" api:"required"`
   201  	// Org-level role. A federation rule may only be created or retargeted to grant
   202  	// `org:admin` scope when this is `admin`. A rule granting `org:admin` whose target
   203  	// is later demoted to `developer` is rejected at token exchange. Rules granting
   204  	// `org:admin` are managed in the Console.
   205  	//
   206  	// Any of "admin", "developer".
   207  	OrganizationRole BetaServiceAccountOrganizationRole `json:"organization_role" api:"required"`
   208  	Type             constant.ServiceAccount            `json:"type" default:"service_account"`
   209  	// When this service account was last updated.
   210  	UpdatedAt time.Time `json:"updated_at" api:"required" format:"date-time"`
   211  	// Tagged ID (`user_`/`svac_`) of the actor that last updated this service account.
   212  	UpdatedByActorID string `json:"updated_by_actor_id" api:"required"`
   213  	// JSON contains metadata for fields, check presence with [respjson.Field.Valid].
   214  	JSON struct {
   215  		ID                respjson.Field
   216  		ArchivedAt        respjson.Field
   217  		ArchivedByActorID respjson.Field
   218  		CreatedAt         respjson.Field
   219  		CreatedByActorID  respjson.Field
   220  		Description       respjson.Field
   221  		Name              respjson.Field
   222  		OrganizationRole  respjson.Field
   223  		Type              respjson.Field
   224  		UpdatedAt         respjson.Field
   225  		UpdatedByActorID  respjson.Field
   226  		ExtraFields       map[string]respjson.Field
   227  		raw               string
   228  	} `json:"-"`
   229  }
   230  
   231  // Returns the unmodified JSON received from the API
   232  func (r BetaServiceAccount) RawJSON() string { return r.JSON.raw }
   233  func (r *BetaServiceAccount) UnmarshalJSON(data []byte) error {
   234  	return apijson.UnmarshalRoot(data, r)
   235  }
   236  
   237  // Org-level role. A federation rule may only be created or retargeted to grant
   238  // `org:admin` scope when this is `admin`. A rule granting `org:admin` whose target
   239  // is later demoted to `developer` is rejected at token exchange. Rules granting
   240  // `org:admin` are managed in the Console.
   241  type BetaServiceAccountOrganizationRole string
   242  
   243  const (
   244  	BetaServiceAccountOrganizationRoleAdmin     BetaServiceAccountOrganizationRole = "admin"
   245  	BetaServiceAccountOrganizationRoleDeveloper BetaServiceAccountOrganizationRole = "developer"
   246  )
   247  
   248  type BetaServiceAccountWorkspaceMember struct {
   249  	// Tagged ID (`user_...`/`svac_...`) of the actor who created this membership.
   250  	CreatedByActorID string `json:"created_by_actor_id" api:"required"`
   251  	// True when this is the implicit default-workspace membership every service
   252  	// account has when no explicit membership exists. Implicit memberships have role
   253  	// `workspace_user` and cannot be removed.
   254  	Implicit bool `json:"implicit" api:"required"`
   255  	// Tagged service account ID (`svac_...`).
   256  	ServiceAccountID string                                 `json:"service_account_id" api:"required"`
   257  	Type             constant.ServiceAccountWorkspaceMember `json:"type" default:"service_account_workspace_member"`
   258  	// Tagged workspace ID (`wrkspc_...`).
   259  	WorkspaceID string `json:"workspace_id" api:"required"`
   260  	// Role of the service account in this workspace. Service accounts cannot hold the
   261  	// `workspace_billing` role.
   262  	//
   263  	// Any of "workspace_admin", "workspace_billing", "workspace_developer",
   264  	// "workspace_restricted_developer", "workspace_user".
   265  	WorkspaceRole BetaWorkspaceRole `json:"workspace_role" api:"required"`
   266  	// JSON contains metadata for fields, check presence with [respjson.Field.Valid].
   267  	JSON struct {
   268  		CreatedByActorID respjson.Field
   269  		Implicit         respjson.Field
   270  		ServiceAccountID respjson.Field
   271  		Type             respjson.Field
   272  		WorkspaceID      respjson.Field
   273  		WorkspaceRole    respjson.Field
   274  		ExtraFields      map[string]respjson.Field
   275  		raw              string
   276  	} `json:"-"`
   277  }
   278  
   279  // Returns the unmodified JSON received from the API
   280  func (r BetaServiceAccountWorkspaceMember) RawJSON() string { return r.JSON.raw }
   281  func (r *BetaServiceAccountWorkspaceMember) UnmarshalJSON(data []byte) error {
   282  	return apijson.UnmarshalRoot(data, r)
   283  }
   284  
   285  type BetaOrganizationServiceAccountNewParams struct {
   286  	// Slug identifier (lowercase, digits, hyphens). Unique within the organization; a
   287  	// duplicate name returns 409.
   288  	Name string `json:"name" api:"required"`
   289  	// Optional free-text description.
   290  	Description param.Opt[string] `json:"description,omitzero"`
   291  	// Org-level role. Defaults to `developer`.
   292  	//
   293  	// Any of "admin", "developer".
   294  	OrganizationRole BetaOrganizationServiceAccountNewParamsOrganizationRole `json:"organization_role,omitzero"`
   295  	// Optional header to specify the beta version(s) you want to use.
   296  	Betas []AnthropicBeta `header:"anthropic-beta,omitzero" json:"-"`
   297  	paramObj
   298  }
   299  
   300  func (r BetaOrganizationServiceAccountNewParams) MarshalJSON() (data []byte, err error) {
   301  	type shadow BetaOrganizationServiceAccountNewParams
   302  	return param.MarshalObject(r, (*shadow)(&r))
   303  }
   304  func (r *BetaOrganizationServiceAccountNewParams) UnmarshalJSON(data []byte) error {
   305  	return apijson.UnmarshalRoot(data, r)
   306  }
   307  
   308  // Org-level role. Defaults to `developer`.
   309  type BetaOrganizationServiceAccountNewParamsOrganizationRole string
   310  
   311  const (
   312  	BetaOrganizationServiceAccountNewParamsOrganizationRoleAdmin     BetaOrganizationServiceAccountNewParamsOrganizationRole = "admin"
   313  	BetaOrganizationServiceAccountNewParamsOrganizationRoleDeveloper BetaOrganizationServiceAccountNewParamsOrganizationRole = "developer"
   314  )
   315  
   316  type BetaOrganizationServiceAccountGetParams struct {
   317  	// Optional header to specify the beta version(s) you want to use.
   318  	Betas []AnthropicBeta `header:"anthropic-beta,omitzero" json:"-"`
   319  	paramObj
   320  }
   321  
   322  type BetaOrganizationServiceAccountUpdateParams struct {
   323  	// Replaces the description. Omit to leave unchanged; send `null` to clear (the
   324  	// field is stored as an empty string).
   325  	Description param.Opt[string] `json:"description,omitzero"`
   326  	// Replaces the org-level role. Omit or send `null` to leave unchanged.
   327  	//
   328  	// Any of "admin", "developer".
   329  	OrganizationRole BetaOrganizationServiceAccountUpdateParamsOrganizationRole `json:"organization_role,omitzero"`
   330  	// Optional header to specify the beta version(s) you want to use.
   331  	Betas []AnthropicBeta `header:"anthropic-beta,omitzero" json:"-"`
   332  	paramObj
   333  }
   334  
   335  func (r BetaOrganizationServiceAccountUpdateParams) MarshalJSON() (data []byte, err error) {
   336  	type shadow BetaOrganizationServiceAccountUpdateParams
   337  	return param.MarshalObject(r, (*shadow)(&r))
   338  }
   339  func (r *BetaOrganizationServiceAccountUpdateParams) UnmarshalJSON(data []byte) error {
   340  	return apijson.UnmarshalRoot(data, r)
   341  }
   342  
   343  // Replaces the org-level role. Omit or send `null` to leave unchanged.
   344  type BetaOrganizationServiceAccountUpdateParamsOrganizationRole string
   345  
   346  const (
   347  	BetaOrganizationServiceAccountUpdateParamsOrganizationRoleAdmin     BetaOrganizationServiceAccountUpdateParamsOrganizationRole = "admin"
   348  	BetaOrganizationServiceAccountUpdateParamsOrganizationRoleDeveloper BetaOrganizationServiceAccountUpdateParamsOrganizationRole = "developer"
   349  )
   350  
   351  type BetaOrganizationServiceAccountListParams struct {
   352  	// Opaque cursor from a previous response's `next_page`.
   353  	Page param.Opt[string] `query:"page,omitzero" json:"-"`
   354  	// Include archived resources. Defaults to false.
   355  	IncludeArchived param.Opt[bool] `query:"include_archived,omitzero" json:"-"`
   356  	// Number of results per page.
   357  	Limit param.Opt[int64] `query:"limit,omitzero" json:"-"`
   358  	// Optional header to specify the beta version(s) you want to use.
   359  	Betas []AnthropicBeta `header:"anthropic-beta,omitzero" json:"-"`
   360  	paramObj
   361  }
   362  
   363  // URLQuery serializes [BetaOrganizationServiceAccountListParams]'s query
   364  // parameters as `url.Values`.
   365  func (r BetaOrganizationServiceAccountListParams) URLQuery() (v url.Values, err error) {
   366  	return apiquery.MarshalWithSettings(r, apiquery.QuerySettings{
   367  		ArrayFormat:  apiquery.ArrayQueryFormatBrackets,
   368  		NestedFormat: apiquery.NestedQueryFormatBrackets,
   369  	})
   370  }
   371  
   372  type BetaOrganizationServiceAccountArchiveParams struct {
   373  	// Optional header to specify the beta version(s) you want to use.
   374  	Betas []AnthropicBeta `header:"anthropic-beta,omitzero" json:"-"`
   375  	paramObj
   376  }