GetAddonIDFromDatabase resolves the addon ID from a database name by calling the API. This is useful when the database name is provided as a positional argument.
(ctx context.Context, databaseName string)
| 20 | // GetAddonIDFromDatabase resolves the addon ID from a database name by calling the API. |
| 21 | // This is useful when the database name is provided as a positional argument. |
| 22 | func GetAddonIDFromDatabase(ctx context.Context, databaseName string) (string, error) { |
| 23 | client, err := config.ScalingoClient(ctx) |
| 24 | if err != nil { |
| 25 | return "", errors.Wrap(ctx, err, "get Scalingo client") |
| 26 | } |
| 27 | |
| 28 | // AddonsList works for both apps and DBNG databases (same API endpoint). |
| 29 | // A DBNG database is modeled as an app with a single addon (itself), |
| 30 | // whereas an application can have multiple addons (postgresql, redis, etc.). |
| 31 | // If multiple addons are returned, the ID is likely an application, not a database. |
| 32 | addons, err := client.AddonsList(ctx, databaseName) |
| 33 | if err != nil { |
| 34 | return "", errors.Wrap(ctx, err, "list addons") |
| 35 | } |
| 36 | |
| 37 | if len(addons) == 0 { |
| 38 | return "", errors.Newf(ctx, "no addon found for database %s", databaseName) |
| 39 | } |
| 40 | |
| 41 | if len(addons) > 1 { |
| 42 | return "", errors.Newf(ctx, "multiple addons found for %s, it may be an application", databaseName) |
| 43 | } |
| 44 | |
| 45 | return addons[0].ID, nil |
| 46 | } |
| 47 | |
| 48 | // ErrTooManyArguments is returned when too many arguments are provided |
| 49 | var ErrTooManyArguments = stderrors.New("too many arguments") |
no test coverage detected