-
Notifications
You must be signed in to change notification settings - Fork 3
/
create.go
69 lines (60 loc) · 2.5 KB
/
create.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package postgresqlcontroller
import (
"context"
"fmt"
exoscalev1 "github.com/vshn/provider-exoscale/apis/exoscale/v1"
"github.com/vshn/provider-exoscale/operator/mapper"
"github.com/crossplane/crossplane-runtime/pkg/errors"
"github.com/crossplane/crossplane-runtime/pkg/reconciler/managed"
"github.com/crossplane/crossplane-runtime/pkg/resource"
"github.com/exoscale/egoscale/v2/oapi"
controllerruntime "sigs.k8s.io/controller-runtime"
)
// Create implements managed.ExternalClient.
func (p *pipeline) Create(ctx context.Context, mg resource.Managed) (managed.ExternalCreation, error) {
log := controllerruntime.LoggerFrom(ctx)
log.Info("Creating resource")
pgInstance := fromManaged(mg)
spec := pgInstance.Spec.ForProvider
body, err := fromSpecToCreateBody(spec)
if err != nil {
return managed.ExternalCreation{}, errors.Wrap(err, "cannot map spec to API request")
}
resp, err := p.exo.CreateDbaasServicePgWithResponse(ctx, oapi.DbaasServiceName(pgInstance.Name), body)
if err != nil {
return managed.ExternalCreation{}, errors.Wrap(err, "cannot create instance")
}
log.V(1).Info("Response", "json", resp.JSON200)
return managed.ExternalCreation{}, nil
}
// fromSpecToCreateBody places the given spec into the request body.
func fromSpecToCreateBody(spec exoscalev1.PostgreSQLParameters) (oapi.CreateDbaasServicePgJSONRequestBody, error) {
/**
NOTE: If you change anything below, also update fromSpecToCreateBody().
Unfortunately the generated openapi-types in exoscale are unusable for reusing same properties.
*/
backupSchedule, err := mapper.ToBackupSchedule(spec.Backup.TimeOfDay)
if err != nil {
return oapi.CreateDbaasServicePgJSONRequestBody{}, fmt.Errorf("invalid backup schedule: %w", err)
}
settings, err := mapper.ToMap(spec.PGSettings)
if err != nil {
return oapi.CreateDbaasServicePgJSONRequestBody{}, fmt.Errorf("invalid pgsettings: %w", err)
}
return oapi.CreateDbaasServicePgJSONRequestBody{
Plan: spec.Size.Plan,
BackupSchedule: &backupSchedule,
Variant: &variantAiven,
Version: &spec.Version,
TerminationProtection: &spec.TerminationProtection,
Maintenance: &struct {
Dow oapi.CreateDbaasServicePgJSONBodyMaintenanceDow `json:"dow"`
Time string `json:"time"`
}{
Dow: oapi.CreateDbaasServicePgJSONBodyMaintenanceDow(spec.Maintenance.DayOfWeek),
Time: spec.Maintenance.TimeOfDay.String(),
},
IpFilter: mapper.ToSlicePtr(spec.IPFilter),
PgSettings: &settings,
}, nil
}