(ctx context.Context, capiUserPrefix string, outputFile string)
| 54 | } |
| 55 | |
| 56 | func (cli *cliCapi) register(ctx context.Context, capiUserPrefix string, outputFile string) error { |
| 57 | cfg := cli.cfg() |
| 58 | |
| 59 | capiUser, err := idgen.GenerateMachineID(capiUserPrefix) |
| 60 | if err != nil { |
| 61 | return fmt.Errorf("unable to generate machine id: %w", err) |
| 62 | } |
| 63 | |
| 64 | pstr, err := idgen.GeneratePassword(idgen.PasswordLength) |
| 65 | if err != nil { |
| 66 | return err |
| 67 | } |
| 68 | |
| 69 | password := strfmt.Password(pstr) |
| 70 | |
| 71 | apiurl, err := url.Parse(CAPIBaseURL) |
| 72 | if err != nil { |
| 73 | return fmt.Errorf("unable to parse api url %s: %w", CAPIBaseURL, err) |
| 74 | } |
| 75 | |
| 76 | _, err = apiclient.RegisterClient(ctx, &apiclient.Config{ |
| 77 | MachineID: capiUser, |
| 78 | Password: password, |
| 79 | URL: apiurl, |
| 80 | VersionPrefix: "v3", |
| 81 | }, nil) |
| 82 | if err != nil { |
| 83 | return fmt.Errorf("api client register ('%s'): %w", CAPIBaseURL, err) |
| 84 | } |
| 85 | |
| 86 | log.Infof("Successfully registered to Central API (CAPI)") |
| 87 | |
| 88 | var dumpFile string |
| 89 | |
| 90 | switch { |
| 91 | case outputFile != "": |
| 92 | dumpFile = outputFile |
| 93 | case cfg.API.Server.OnlineClient.CredentialsFilePath != "": |
| 94 | dumpFile = cfg.API.Server.OnlineClient.CredentialsFilePath |
| 95 | default: |
| 96 | dumpFile = "" |
| 97 | } |
| 98 | |
| 99 | apiCfg := csconfig.ApiCredentialsCfg{ |
| 100 | Login: capiUser, |
| 101 | Password: password.String(), |
| 102 | URL: CAPIBaseURL, |
| 103 | } |
| 104 | |
| 105 | apiConfigDump, err := yaml.Marshal(apiCfg) |
| 106 | if err != nil { |
| 107 | return fmt.Errorf("unable to serialize api credentials: %w", err) |
| 108 | } |
| 109 | |
| 110 | if dumpFile != "" { |
| 111 | err = os.WriteFile(dumpFile, apiConfigDump, 0o600) |
| 112 | if err != nil { |
| 113 | return fmt.Errorf("write api credentials in '%s' failed: %w", dumpFile, err) |
no test coverage detected