github.com/anthropics/anthropic-sdk-go@v1.70.1/betaorganizationworkspaceserviceaccount.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  
    13  	"github.com/anthropics/anthropic-sdk-go/internal/apijson"
    14  	"github.com/anthropics/anthropic-sdk-go/internal/apiquery"
    15  	"github.com/anthropics/anthropic-sdk-go/internal/requestconfig"
    16  	"github.com/anthropics/anthropic-sdk-go/option"
    17  	"github.com/anthropics/anthropic-sdk-go/packages/pagination"
    18  	"github.com/anthropics/anthropic-sdk-go/packages/param"
    19  	"github.com/anthropics/anthropic-sdk-go/packages/respjson"
    20  	"github.com/anthropics/anthropic-sdk-go/shared/constant"
    21  )
    22  
    23  // BetaOrganizationWorkspaceServiceAccountService contains methods and other
    24  // services that help with interacting with the anthropic API.
    25  //
    26  // Note, unlike clients, this service does not read variables from the environment
    27  // automatically. You should not instantiate this service directly, and instead use
    28  // the [NewBetaOrganizationWorkspaceServiceAccountService] method instead.
    29  type BetaOrganizationWorkspaceServiceAccountService struct {
    30  	Options []option.RequestOption
    31  }
    32  
    33  // NewBetaOrganizationWorkspaceServiceAccountService generates a new service that
    34  // applies the given options to each request. These options are applied after the
    35  // parent client's options (if there is one), and before any request-specific
    36  // options.
    37  func NewBetaOrganizationWorkspaceServiceAccountService(opts ...option.RequestOption) (r BetaOrganizationWorkspaceServiceAccountService) {
    38  	r = BetaOrganizationWorkspaceServiceAccountService{}
    39  	r.Options = opts
    40  	return
    41  }
    42  
    43  // **Requires an OAuth access token with the `org:admin` scope**, from
    44  // `ant auth login --scope org:admin` or a workload identity federation rule; Admin
    45  // API keys are not accepted. See
    46  // [Manage WIF with the Admin API](/docs/en/manage-claude/wif-admin-api).
    47  //
    48  // Retrieve a service account's membership in a workspace.
    49  //
    50  // Returns the membership record, including the service account's `workspace_role`
    51  // in this workspace. Archived workspaces return 400. For the default workspace,
    52  // returns the implicit (`implicit: true`) membership when no explicit membership
    53  // exists; an explicitly added membership is returned with its assigned role. An
    54  // archived service account returns 404.
    55  func (r *BetaOrganizationWorkspaceServiceAccountService) Get(ctx context.Context, serviceAccountID string, params BetaOrganizationWorkspaceServiceAccountGetParams, opts ...option.RequestOption) (res *BetaServiceAccountWorkspaceMember, err error) {
    56  	for _, v := range params.Betas {
    57  		opts = append(opts, option.WithHeaderAdd("anthropic-beta", fmt.Sprintf("%v", v)))
    58  	}
    59  	opts = slices.Concat(r.Options, opts)
    60  	if params.WorkspaceID == "" {
    61  		err = errors.New("missing required workspace_id parameter")
    62  		return nil, err
    63  	}
    64  	if serviceAccountID == "" {
    65  		err = errors.New("missing required service_account_id parameter")
    66  		return nil, err
    67  	}
    68  	path := fmt.Sprintf("v1/organizations/workspaces/%s/service_accounts/%s?beta=true", params.WorkspaceID, serviceAccountID)
    69  	err = requestconfig.ExecuteNewRequest(ctx, http.MethodGet, path, nil, &res, opts...)
    70  	return res, err
    71  }
    72  
    73  // **Requires an OAuth access token with the `org:admin` scope**, from
    74  // `ant auth login --scope org:admin` or a workload identity federation rule; Admin
    75  // API keys are not accepted. See
    76  // [Manage WIF with the Admin API](/docs/en/manage-claude/wif-admin-api).
    77  //
    78  // Change a service account's role in a workspace.
    79  //
    80  // The new `workspace_role` replaces the current one. Only explicit memberships can
    81  // be updated; to set a role on the implicit default-workspace membership, add the
    82  // service account explicitly with
    83  // `POST /workspaces/{workspace_id}/service_accounts`. Archived workspaces
    84  // return 400. Archived service accounts cannot be updated and are rejected.
    85  func (r *BetaOrganizationWorkspaceServiceAccountService) Update(ctx context.Context, serviceAccountID string, params BetaOrganizationWorkspaceServiceAccountUpdateParams, opts ...option.RequestOption) (res *BetaServiceAccountWorkspaceMember, err error) {
    86  	for _, v := range params.Betas {
    87  		opts = append(opts, option.WithHeaderAdd("anthropic-beta", fmt.Sprintf("%v", v)))
    88  	}
    89  	opts = slices.Concat(r.Options, opts)
    90  	if params.WorkspaceID == "" {
    91  		err = errors.New("missing required workspace_id parameter")
    92  		return nil, err
    93  	}
    94  	if serviceAccountID == "" {
    95  		err = errors.New("missing required service_account_id parameter")
    96  		return nil, err
    97  	}
    98  	path := fmt.Sprintf("v1/organizations/workspaces/%s/service_accounts/%s?beta=true", params.WorkspaceID, serviceAccountID)
    99  	err = requestconfig.ExecuteNewRequest(ctx, http.MethodPost, path, params, &res, opts...)
   100  	return res, err
   101  }
   102  
   103  // **Requires an OAuth access token with the `org:admin` scope**, from
   104  // `ant auth login --scope org:admin` or a workload identity federation rule; Admin
   105  // API keys are not accepted. See
   106  // [Manage WIF with the Admin API](/docs/en/manage-claude/wif-admin-api).
   107  //
   108  // List the service accounts that are members of a workspace.
   109  //
   110  // Each entry includes the service account's `workspace_role`. Use `limit` and the
   111  // `next_page` cursor to paginate. Archived workspaces return 400; use
   112  // `GET /service_accounts/{id}/workspaces` to audit memberships of an archived
   113  // workspace. The implicit default-workspace membership is not included in this
   114  // list. Memberships of archived service accounts are omitted from the results.
   115  func (r *BetaOrganizationWorkspaceServiceAccountService) List(ctx context.Context, workspaceID string, params BetaOrganizationWorkspaceServiceAccountListParams, opts ...option.RequestOption) (res *pagination.PageCursor[BetaServiceAccountWorkspaceMember], err error) {
   116  	var raw *http.Response
   117  	for _, v := range params.Betas {
   118  		opts = append(opts, option.WithHeaderAdd("anthropic-beta", fmt.Sprintf("%v", v)))
   119  	}
   120  	opts = slices.Concat(r.Options, opts)
   121  	opts = append([]option.RequestOption{option.WithResponseInto(&raw)}, opts...)
   122  	if workspaceID == "" {
   123  		err = errors.New("missing required workspace_id parameter")
   124  		return nil, err
   125  	}
   126  	path := fmt.Sprintf("v1/organizations/workspaces/%s/service_accounts?beta=true", workspaceID)
   127  	cfg, err := requestconfig.NewRequestConfig(ctx, http.MethodGet, path, params, &res, opts...)
   128  	if err != nil {
   129  		return nil, err
   130  	}
   131  	err = cfg.Execute()
   132  	if err != nil {
   133  		return nil, err
   134  	}
   135  	res.SetPageConfig(cfg, raw)
   136  	return res, nil
   137  }
   138  
   139  // **Requires an OAuth access token with the `org:admin` scope**, from
   140  // `ant auth login --scope org:admin` or a workload identity federation rule; Admin
   141  // API keys are not accepted. See
   142  // [Manage WIF with the Admin API](/docs/en/manage-claude/wif-admin-api).
   143  //
   144  // List the service accounts that are members of a workspace.
   145  //
   146  // Each entry includes the service account's `workspace_role`. Use `limit` and the
   147  // `next_page` cursor to paginate. Archived workspaces return 400; use
   148  // `GET /service_accounts/{id}/workspaces` to audit memberships of an archived
   149  // workspace. The implicit default-workspace membership is not included in this
   150  // list. Memberships of archived service accounts are omitted from the results.
   151  func (r *BetaOrganizationWorkspaceServiceAccountService) ListAutoPaging(ctx context.Context, workspaceID string, params BetaOrganizationWorkspaceServiceAccountListParams, opts ...option.RequestOption) *pagination.PageCursorAutoPager[BetaServiceAccountWorkspaceMember] {
   152  	return pagination.NewPageCursorAutoPager(r.List(ctx, workspaceID, params, opts...))
   153  }
   154  
   155  // **Requires an OAuth access token with the `org:admin` scope**, from
   156  // `ant auth login --scope org:admin` or a workload identity federation rule; Admin
   157  // API keys are not accepted. See
   158  // [Manage WIF with the Admin API](/docs/en/manage-claude/wif-admin-api).
   159  //
   160  // Add a service account to a workspace with the given `workspace_role`.
   161  //
   162  // The role determines what the service account can do in the workspace and which
   163  // workspace-scoped permissions it can be granted when authenticating through
   164  // federation. Every service account is already an implicit `workspace_user` member
   165  // of the default workspace; adding it explicitly assigns a chosen role. If the
   166  // service account is already an explicit member of the workspace, its
   167  // `workspace_role` is replaced with the value supplied here. Archived workspaces
   168  // return 400. Archived service accounts cannot be added and are rejected.
   169  func (r *BetaOrganizationWorkspaceServiceAccountService) Add(ctx context.Context, workspaceID string, params BetaOrganizationWorkspaceServiceAccountAddParams, opts ...option.RequestOption) (res *BetaServiceAccountWorkspaceMember, err error) {
   170  	for _, v := range params.Betas {
   171  		opts = append(opts, option.WithHeaderAdd("anthropic-beta", fmt.Sprintf("%v", v)))
   172  	}
   173  	opts = slices.Concat(r.Options, opts)
   174  	if workspaceID == "" {
   175  		err = errors.New("missing required workspace_id parameter")
   176  		return nil, err
   177  	}
   178  	path := fmt.Sprintf("v1/organizations/workspaces/%s/service_accounts?beta=true", workspaceID)
   179  	err = requestconfig.ExecuteNewRequest(ctx, http.MethodPost, path, params, &res, opts...)
   180  	return res, err
   181  }
   182  
   183  // **Requires an OAuth access token with the `org:admin` scope**, from
   184  // `ant auth login --scope org:admin` or a workload identity federation rule; Admin
   185  // API keys are not accepted. See
   186  // [Manage WIF with the Admin API](/docs/en/manage-claude/wif-admin-api).
   187  //
   188  // Remove a service account from a workspace.
   189  //
   190  // Removal is idempotent (returns 200 even if the membership was already removed).
   191  // A DELETE against the implicit default-workspace membership returns 200 but is a
   192  // no-op and the membership persists; deleting an explicit default-workspace row
   193  // reverts to the implicit `workspace_user` membership. Archived workspaces
   194  // return 400.
   195  func (r *BetaOrganizationWorkspaceServiceAccountService) Remove(ctx context.Context, serviceAccountID string, params BetaOrganizationWorkspaceServiceAccountRemoveParams, opts ...option.RequestOption) (res *BetaOrganizationWorkspaceServiceAccountRemoveResponse, err error) {
   196  	for _, v := range params.Betas {
   197  		opts = append(opts, option.WithHeaderAdd("anthropic-beta", fmt.Sprintf("%v", v)))
   198  	}
   199  	opts = slices.Concat(r.Options, opts)
   200  	if params.WorkspaceID == "" {
   201  		err = errors.New("missing required workspace_id parameter")
   202  		return nil, err
   203  	}
   204  	if serviceAccountID == "" {
   205  		err = errors.New("missing required service_account_id parameter")
   206  		return nil, err
   207  	}
   208  	path := fmt.Sprintf("v1/organizations/workspaces/%s/service_accounts/%s?beta=true", params.WorkspaceID, serviceAccountID)
   209  	err = requestconfig.ExecuteNewRequest(ctx, http.MethodDelete, path, nil, &res, opts...)
   210  	return res, err
   211  }
   212  
   213  type BetaOrganizationWorkspaceServiceAccountRemoveResponse struct {
   214  	// Tagged service account ID (`svac_...`) named in the delete request. Removal is
   215  	// idempotent; see the endpoint description for the implicit-membership no-op.
   216  	ServiceAccountID string                                        `json:"service_account_id" api:"required"`
   217  	Type             constant.ServiceAccountWorkspaceMemberDeleted `json:"type" default:"service_account_workspace_member_deleted"`
   218  	// Tagged workspace ID (`wrkspc_...`) named in the delete request.
   219  	WorkspaceID string `json:"workspace_id" api:"required"`
   220  	// JSON contains metadata for fields, check presence with [respjson.Field.Valid].
   221  	JSON struct {
   222  		ServiceAccountID respjson.Field
   223  		Type             respjson.Field
   224  		WorkspaceID      respjson.Field
   225  		ExtraFields      map[string]respjson.Field
   226  		raw              string
   227  	} `json:"-"`
   228  }
   229  
   230  // Returns the unmodified JSON received from the API
   231  func (r BetaOrganizationWorkspaceServiceAccountRemoveResponse) RawJSON() string { return r.JSON.raw }
   232  func (r *BetaOrganizationWorkspaceServiceAccountRemoveResponse) UnmarshalJSON(data []byte) error {
   233  	return apijson.UnmarshalRoot(data, r)
   234  }
   235  
   236  type BetaOrganizationWorkspaceServiceAccountGetParams struct {
   237  	// ID of the workspace.
   238  	WorkspaceID string `path:"workspace_id" api:"required" json:"-"`
   239  	// Optional header to specify the beta version(s) you want to use.
   240  	Betas []AnthropicBeta `header:"anthropic-beta,omitzero" json:"-"`
   241  	paramObj
   242  }
   243  
   244  type BetaOrganizationWorkspaceServiceAccountUpdateParams struct {
   245  	// ID of the workspace.
   246  	WorkspaceID string `path:"workspace_id" api:"required" json:"-"`
   247  	// New role for the service account in this workspace.
   248  	//
   249  	// Any of "workspace_admin", "workspace_developer",
   250  	// "workspace_restricted_developer", "workspace_user".
   251  	WorkspaceRole BetaNoBillingWorkspaceRole `json:"workspace_role,omitzero" api:"required"`
   252  	// Optional header to specify the beta version(s) you want to use.
   253  	Betas []AnthropicBeta `header:"anthropic-beta,omitzero" json:"-"`
   254  	paramObj
   255  }
   256  
   257  func (r BetaOrganizationWorkspaceServiceAccountUpdateParams) MarshalJSON() (data []byte, err error) {
   258  	type shadow BetaOrganizationWorkspaceServiceAccountUpdateParams
   259  	return param.MarshalObject(r, (*shadow)(&r))
   260  }
   261  func (r *BetaOrganizationWorkspaceServiceAccountUpdateParams) UnmarshalJSON(data []byte) error {
   262  	return apijson.UnmarshalRoot(data, r)
   263  }
   264  
   265  type BetaOrganizationWorkspaceServiceAccountListParams struct {
   266  	// Opaque cursor from a previous response's `next_page`.
   267  	Page param.Opt[string] `query:"page,omitzero" json:"-"`
   268  	// Number of results per page.
   269  	Limit param.Opt[int64] `query:"limit,omitzero" json:"-"`
   270  	// Optional header to specify the beta version(s) you want to use.
   271  	Betas []AnthropicBeta `header:"anthropic-beta,omitzero" json:"-"`
   272  	paramObj
   273  }
   274  
   275  // URLQuery serializes [BetaOrganizationWorkspaceServiceAccountListParams]'s query
   276  // parameters as `url.Values`.
   277  func (r BetaOrganizationWorkspaceServiceAccountListParams) URLQuery() (v url.Values, err error) {
   278  	return apiquery.MarshalWithSettings(r, apiquery.QuerySettings{
   279  		ArrayFormat:  apiquery.ArrayQueryFormatBrackets,
   280  		NestedFormat: apiquery.NestedQueryFormatBrackets,
   281  	})
   282  }
   283  
   284  type BetaOrganizationWorkspaceServiceAccountAddParams struct {
   285  	// Tagged service account ID to add.
   286  	ServiceAccountID string `json:"service_account_id" api:"required"`
   287  	// Role to assign to the service account in this workspace.
   288  	//
   289  	// Any of "workspace_admin", "workspace_developer",
   290  	// "workspace_restricted_developer", "workspace_user".
   291  	WorkspaceRole BetaNoBillingWorkspaceRole `json:"workspace_role,omitzero" api:"required"`
   292  	// Optional header to specify the beta version(s) you want to use.
   293  	Betas []AnthropicBeta `header:"anthropic-beta,omitzero" json:"-"`
   294  	paramObj
   295  }
   296  
   297  func (r BetaOrganizationWorkspaceServiceAccountAddParams) MarshalJSON() (data []byte, err error) {
   298  	type shadow BetaOrganizationWorkspaceServiceAccountAddParams
   299  	return param.MarshalObject(r, (*shadow)(&r))
   300  }
   301  func (r *BetaOrganizationWorkspaceServiceAccountAddParams) UnmarshalJSON(data []byte) error {
   302  	return apijson.UnmarshalRoot(data, r)
   303  }
   304  
   305  type BetaOrganizationWorkspaceServiceAccountRemoveParams struct {
   306  	// ID of the workspace.
   307  	WorkspaceID string `path:"workspace_id" api:"required" json:"-"`
   308  	// Optional header to specify the beta version(s) you want to use.
   309  	Betas []AnthropicBeta `header:"anthropic-beta,omitzero" json:"-"`
   310  	paramObj
   311  }