|
| 1 | +//go:build cli |
| 2 | + |
| 3 | +package directoryaccount |
| 4 | + |
| 5 | +import ( |
| 6 | + "context" |
| 7 | + |
| 8 | + "github.com/spf13/cobra" |
| 9 | + |
| 10 | + "github.com/theopenlane/core/cli/cmd" |
| 11 | + "github.com/theopenlane/go-client/graphclient" |
| 12 | +) |
| 13 | + |
| 14 | +var createCmd = &cobra.Command{ |
| 15 | + Use: "create", |
| 16 | + Short: "create a new directory account", |
| 17 | + Run: func(cmd *cobra.Command, args []string) { |
| 18 | + err := create(cmd.Context()) |
| 19 | + cobra.CheckErr(err) |
| 20 | + }, |
| 21 | +} |
| 22 | + |
| 23 | +func init() { |
| 24 | + command.AddCommand(createCmd) |
| 25 | + |
| 26 | + createCmd.Flags().StringP("external-id", "e", "", "stable identifier from the directory system") |
| 27 | + createCmd.Flags().String("canonical-email", "", "primary email address of the account") |
| 28 | + createCmd.Flags().String("display-name", "", "display name of the account") |
| 29 | + createCmd.Flags().String("given-name", "", "first name reported by the provider") |
| 30 | + createCmd.Flags().String("family-name", "", "last name reported by the provider") |
| 31 | + createCmd.Flags().String("job-title", "", "title captured at sync time") |
| 32 | + createCmd.Flags().String("department", "", "department captured at sync time") |
| 33 | + createCmd.Flags().String("directory-name", "", "directory source label (e.g. googleworkspace, github, slack)") |
| 34 | + createCmd.Flags().String("directory-instance-id", "", "stable external workspace or tenant identifier") |
| 35 | + createCmd.Flags().String("integration-id", "", "integration that owns this directory account") |
| 36 | + createCmd.Flags().String("directory-sync-run-id", "", "sync run that produced this snapshot") |
| 37 | + createCmd.Flags().String("platform-id", "", "platform associated with this directory account") |
| 38 | + createCmd.Flags().StringSlice("tags", []string{}, "tags associated with the account") |
| 39 | +} |
| 40 | + |
| 41 | +// createValidation validates the required fields for the command |
| 42 | +func createValidation() (input graphclient.CreateDirectoryAccountInput, err error) { |
| 43 | + externalID := cmd.Config.String("external-id") |
| 44 | + if externalID == "" { |
| 45 | + return input, cmd.NewRequiredFieldMissingError("external id") |
| 46 | + } |
| 47 | + |
| 48 | + input.ExternalID = externalID |
| 49 | + |
| 50 | + canonicalEmail := cmd.Config.String("canonical-email") |
| 51 | + if canonicalEmail != "" { |
| 52 | + input.CanonicalEmail = &canonicalEmail |
| 53 | + } |
| 54 | + |
| 55 | + displayName := cmd.Config.String("display-name") |
| 56 | + if displayName != "" { |
| 57 | + input.DisplayName = &displayName |
| 58 | + } |
| 59 | + |
| 60 | + givenName := cmd.Config.String("given-name") |
| 61 | + if givenName != "" { |
| 62 | + input.GivenName = &givenName |
| 63 | + } |
| 64 | + |
| 65 | + familyName := cmd.Config.String("family-name") |
| 66 | + if familyName != "" { |
| 67 | + input.FamilyName = &familyName |
| 68 | + } |
| 69 | + |
| 70 | + jobTitle := cmd.Config.String("job-title") |
| 71 | + if jobTitle != "" { |
| 72 | + input.JobTitle = &jobTitle |
| 73 | + } |
| 74 | + |
| 75 | + department := cmd.Config.String("department") |
| 76 | + if department != "" { |
| 77 | + input.Department = &department |
| 78 | + } |
| 79 | + |
| 80 | + directoryName := cmd.Config.String("directory-name") |
| 81 | + if directoryName != "" { |
| 82 | + input.DirectoryName = &directoryName |
| 83 | + } |
| 84 | + |
| 85 | + directoryInstanceID := cmd.Config.String("directory-instance-id") |
| 86 | + if directoryInstanceID != "" { |
| 87 | + input.DirectoryInstanceID = &directoryInstanceID |
| 88 | + } |
| 89 | + |
| 90 | + integrationID := cmd.Config.String("integration-id") |
| 91 | + if integrationID != "" { |
| 92 | + input.IntegrationID = &integrationID |
| 93 | + } |
| 94 | + |
| 95 | + directorySyncRunID := cmd.Config.String("directory-sync-run-id") |
| 96 | + if directorySyncRunID != "" { |
| 97 | + input.DirectorySyncRunID = &directorySyncRunID |
| 98 | + } |
| 99 | + |
| 100 | + platformID := cmd.Config.String("platform-id") |
| 101 | + if platformID != "" { |
| 102 | + input.PlatformID = &platformID |
| 103 | + } |
| 104 | + |
| 105 | + tags := cmd.Config.Strings("tags") |
| 106 | + if len(tags) > 0 { |
| 107 | + input.Tags = tags |
| 108 | + } |
| 109 | + |
| 110 | + return input, nil |
| 111 | +} |
| 112 | + |
| 113 | +// create a new directory account |
| 114 | +func create(ctx context.Context) error { |
| 115 | + client, err := cmd.TokenAuth(ctx, cmd.Config) |
| 116 | + if err != nil || client == nil { |
| 117 | + client, err = cmd.SetupClientWithAuth(ctx) |
| 118 | + cobra.CheckErr(err) |
| 119 | + defer cmd.StoreSessionCookies(client) |
| 120 | + } |
| 121 | + |
| 122 | + input, err := createValidation() |
| 123 | + cobra.CheckErr(err) |
| 124 | + |
| 125 | + o, err := client.CreateDirectoryAccount(ctx, input) |
| 126 | + cobra.CheckErr(err) |
| 127 | + |
| 128 | + return consoleOutput(o) |
| 129 | +} |
0 commit comments