CreateClient returns a new agent client from the command context. It works just like InitClient, but uses the agent token and URL instead.
()
| 966 | // CreateClient returns a new agent client from the command context. It works |
| 967 | // just like InitClient, but uses the agent token and URL instead. |
| 968 | func (a *AgentAuth) CreateClient() (*agentsdk.Client, error) { |
| 969 | agentURL := a.agentURL |
| 970 | if agentURL.String() == "" { |
| 971 | return nil, xerrors.Errorf("%s must be set", envAgentURL) |
| 972 | } |
| 973 | |
| 974 | var iiOpts []agentsdk.InstanceIdentityOption |
| 975 | if a.agentName != "" { |
| 976 | iiOpts = append(iiOpts, agentsdk.WithInstanceIdentityAgentName(a.agentName)) |
| 977 | } |
| 978 | |
| 979 | switch a.agentAuth { |
| 980 | case "token": |
| 981 | token := a.agentToken |
| 982 | if token == "" { |
| 983 | if a.agentTokenFile == "" { |
| 984 | return nil, xerrors.Errorf("Either %s or %s must be set", envAgentToken, envAgentTokenFile) |
| 985 | } |
| 986 | tokenBytes, err := os.ReadFile(a.agentTokenFile) |
| 987 | if err != nil { |
| 988 | return nil, xerrors.Errorf("read token file %q: %w", a.agentTokenFile, err) |
| 989 | } |
| 990 | token = strings.TrimSpace(string(tokenBytes)) |
| 991 | } |
| 992 | if token == "" { |
| 993 | return nil, xerrors.Errorf("CODER_AGENT_TOKEN or CODER_AGENT_TOKEN_FILE must be set for token auth") |
| 994 | } |
| 995 | return agentsdk.New(&a.agentURL, agentsdk.WithFixedToken(token)), nil |
| 996 | case "google-instance-identity": |
| 997 | return agentsdk.New(&a.agentURL, agentsdk.WithGoogleInstanceIdentity("", nil, iiOpts...)), nil |
| 998 | case "aws-instance-identity": |
| 999 | return agentsdk.New(&a.agentURL, agentsdk.WithAWSInstanceIdentity(iiOpts...)), nil |
| 1000 | case "azure-instance-identity": |
| 1001 | return agentsdk.New(&a.agentURL, agentsdk.WithAzureInstanceIdentity(iiOpts...)), nil |
| 1002 | default: |
| 1003 | return nil, xerrors.Errorf("unknown agent auth type: %s", a.agentAuth) |
| 1004 | } |
| 1005 | } |
| 1006 | |
| 1007 | type OrganizationContext struct { |
| 1008 | // FlagSelect is the value passed in via the --org flag |