Skip to content

Commit

Permalink
Merge pull request #71 from vshn/fix/item_desc_vshn_services
Browse files Browse the repository at this point in the history
Set proper Zone or Cluster description for odoo billing export
  • Loading branch information
TheBigLee committed Dec 22, 2023
2 parents 868cd23 + fdf5a6b commit 6c9af3a
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 15 deletions.
8 changes: 5 additions & 3 deletions pkg/cloudscale/objectstorage.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,20 +28,22 @@ type ObjectStorage struct {
controlApiClient k8s.Client
salesOrder string
clusterId string
cloudZone string
uomMapping map[string]string
}

const (
namespaceLabel = "crossplane.io/claim-namespace"
)

func NewObjectStorage(client *cloudscale.Client, k8sClient k8s.Client, controlApiClient k8s.Client, salesOrder, clusterId string, uomMapping map[string]string) (*ObjectStorage, error) {
func NewObjectStorage(client *cloudscale.Client, k8sClient k8s.Client, controlApiClient k8s.Client, salesOrder, clusterId string, cloudZone string, uomMapping map[string]string) (*ObjectStorage, error) {
return &ObjectStorage{
client: client,
k8sClient: k8sClient,
controlApiClient: controlApiClient,
salesOrder: salesOrder,
clusterId: clusterId,
cloudZone: cloudZone,
uomMapping: uomMapping,
}, nil
}
Expand Down Expand Up @@ -125,9 +127,9 @@ func (o *ObjectStorage) createOdooRecord(bucketMetricsData cloudscale.BucketMetr

itemGroup := ""
if appuioManaged {
itemGroup = fmt.Sprintf("APPUiO Managed - Zone: %s / Namespace: %s", o.clusterId, b.Namespace)
itemGroup = fmt.Sprintf("APPUiO Managed - Cluster: %s / Namespace: %s", o.clusterId, b.Namespace)
} else {
itemGroup = fmt.Sprintf("APPUiO Cloud - Zone: %s / Namespace: %s", o.clusterId, b.Namespace)
itemGroup = fmt.Sprintf("APPUiO Cloud - Zone: %s / Namespace: %s", o.cloudZone, b.Namespace)
}

instanceId := fmt.Sprintf("%s/%s", b.Zone, bucketMetricsData.Subject.BucketName)
Expand Down
5 changes: 4 additions & 1 deletion pkg/cmd/cloudscale.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ func CloudscaleCmds() *cli.Command {
odooClientSecret string
salesOrder string
clusterId string
cloudZone string
uom string
)
return &cli.Command{
Expand Down Expand Up @@ -62,6 +63,8 @@ func CloudscaleCmds() *cli.Command {
EnvVars: []string{"APPUIO_MANAGED_SALES_ORDER"}, Destination: &salesOrder, Required: false, DefaultText: defaultTextForOptionalFlags},
&cli.StringFlag{Name: "cluster-id", Usage: "The cluster id to save in the billing record",
EnvVars: []string{"CLUSTER_ID"}, Destination: &clusterId, Required: true, DefaultText: defaultTextForRequiredFlags},
&cli.StringFlag{Name: "cluster-zone", Usage: "The cluster zone to save in the billing record",
EnvVars: []string{"CLOUD_ZONE"}, Destination: &cloudZone, Required: false, DefaultText: defaultTextForOptionalFlags},
&cli.StringFlag{Name: "uom", Usage: "Unit of measure mapping between cloud services and Odoo16 in json format",
EnvVars: []string{"UOM"}, Destination: &uom, Required: true, DefaultText: defaultTextForRequiredFlags},
&cli.IntFlag{Name: "collect-interval", Usage: "How often to collect the metrics from the Cloud Service in hours - 1-23",
Expand Down Expand Up @@ -106,7 +109,7 @@ func CloudscaleCmds() *cli.Command {
return fmt.Errorf("load loaction: %w", err)
}

o, err := cs.NewObjectStorage(cloudscaleClient, k8sClient, k8sControlClient, salesOrder, clusterId, mapping)
o, err := cs.NewObjectStorage(cloudscaleClient, k8sClient, k8sControlClient, salesOrder, clusterId, cloudZone, mapping)
if err != nil {
return fmt.Errorf("object storage: %w", err)
}
Expand Down
7 changes: 5 additions & 2 deletions pkg/cmd/exoscale.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ func ExoscaleCmds() *cli.Command {
odooClientSecret string
salesOrder string
clusterId string
cloudZone string
uom string
// For dbaas in minutes
// For objectstorage in hours
Expand Down Expand Up @@ -69,6 +70,8 @@ func ExoscaleCmds() *cli.Command {
EnvVars: []string{"BILLING_HOUR"}, Destination: &billingHour, Required: false, DefaultText: defaultTextForOptionalFlags},
&cli.StringFlag{Name: "cluster-id", Usage: "The cluster id to save in the billing record",
EnvVars: []string{"CLUSTER_ID"}, Destination: &clusterId, Required: true, DefaultText: defaultTextForRequiredFlags},
&cli.StringFlag{Name: "cluster-zone", Usage: "The cluster zone to save in the billing record",
EnvVars: []string{"CLOUD_ZONE"}, Destination: &cloudZone, Required: false, DefaultText: defaultTextForOptionalFlags},
&cli.StringFlag{Name: "uom", Usage: "Unit of measure mapping between cloud services and Odoo16 in json format",
EnvVars: []string{"UOM"}, Destination: &uom, Required: true, DefaultText: defaultTextForRequiredFlags},
},
Expand Down Expand Up @@ -116,7 +119,7 @@ func ExoscaleCmds() *cli.Command {
collectInterval = 23
}

o, err := exoscale.NewObjectStorage(exoscaleClient, k8sClient, k8sControlClient, salesOrder, clusterId, mapping)
o, err := exoscale.NewObjectStorage(exoscaleClient, k8sClient, k8sControlClient, salesOrder, clusterId, cloudZone, mapping)
if err != nil {
return fmt.Errorf("objectbucket service: %w", err)
}
Expand Down Expand Up @@ -196,7 +199,7 @@ func ExoscaleCmds() *cli.Command {
collectInterval = 1
}

d, err := exoscale.NewDBaaS(exoscaleClient, k8sClient, k8sControlClient, collectInterval, salesOrder, clusterId, mapping)
d, err := exoscale.NewDBaaS(exoscaleClient, k8sClient, k8sControlClient, collectInterval, salesOrder, clusterId, cloudZone, mapping)
if err != nil {
return fmt.Errorf("dbaas service: %w", err)
}
Expand Down
8 changes: 5 additions & 3 deletions pkg/exoscale/dbaas.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,18 +68,20 @@ type DBaaS struct {
controlApiClient k8s.Client
salesOrder string
clusterId string
cloudZone string
collectInterval int
uomMapping map[string]string
}

// NewDBaaS creates a Service with the initial setup
func NewDBaaS(exoscaleClient *egoscale.Client, k8sClient k8s.Client, controlApiClient k8s.Client, collectInterval int, salesOrder, clusterId string, uomMapping map[string]string) (*DBaaS, error) {
func NewDBaaS(exoscaleClient *egoscale.Client, k8sClient k8s.Client, controlApiClient k8s.Client, collectInterval int, salesOrder, clusterId string, cloudZone string, uomMapping map[string]string) (*DBaaS, error) {
return &DBaaS{
exoscaleClient: exoscaleClient,
k8sClient: k8sClient,
controlApiClient: controlApiClient,
salesOrder: salesOrder,
clusterId: clusterId,
cloudZone: cloudZone,
collectInterval: collectInterval,
uomMapping: uomMapping,
}, nil
Expand Down Expand Up @@ -207,11 +209,11 @@ func (ds *DBaaS) AggregateDBaaS(ctx context.Context, exoscaleDBaaS []*egoscale.D
if exists && dbaasDetail.Kind == groupVersionKinds[*dbaasUsage.Type].Kind {
logger.V(1).Info("Found exoscale dbaas usage", "instance", dbaasUsage.Name, "instance created", dbaasUsage.CreatedAt)

itemGroup := fmt.Sprintf("APPUiO Managed - Zone: %s / Namespace: %s", ds.clusterId, dbaasDetail.Namespace)
itemGroup := fmt.Sprintf("APPUiO Managed - Cluster: %s / Namespace: %s", ds.clusterId, dbaasDetail.Namespace)
instanceId := fmt.Sprintf("%s/%s", dbaasDetail.Zone, dbaasDetail.DBName)
salesOrder := ds.salesOrder
if salesOrder == "" {
itemGroup = fmt.Sprintf("APPUiO Cloud - Zone: %s / Namespace: %s", ds.clusterId, dbaasDetail.Namespace)
itemGroup = fmt.Sprintf("APPUiO Cloud - Zone: %s / Namespace: %s", ds.cloudZone, dbaasDetail.Namespace)
salesOrder, err = controlAPI.GetSalesOrder(ctx, ds.controlApiClient, dbaasDetail.Organization)
if err != nil {
logger.Error(err, "Unable to sync DBaaS, cannot get salesOrder", "namespace", dbaasDetail.Namespace)
Expand Down
6 changes: 3 additions & 3 deletions pkg/exoscale/dbaas_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func TestDBaaS_aggregatedDBaaS(t *testing.T) {
ProductID: "appcat-exoscale-pg-hobbyist-2",
InstanceID: "ch-gva-2/postgres-abc",
ItemDescription: "Exoscale DBaaS PostgreSQL",
ItemGroupDescription: "APPUiO Managed - Zone: c-test1 / Namespace: vshn-xyz",
ItemGroupDescription: "APPUiO Managed - Cluster: c-test1 / Namespace: vshn-xyz",
SalesOrder: "1234",
UnitID: "",
ConsumedUnits: 1,
Expand All @@ -35,7 +35,7 @@ func TestDBaaS_aggregatedDBaaS(t *testing.T) {
ProductID: "appcat-exoscale-pg-business-128",
InstanceID: "ch-gva-2/postgres-def",
ItemDescription: "Exoscale DBaaS PostgreSQL",
ItemGroupDescription: "APPUiO Managed - Zone: c-test1 / Namespace: vshn-uvw",
ItemGroupDescription: "APPUiO Managed - Cluster: c-test1 / Namespace: vshn-uvw",
SalesOrder: "1234",
UnitID: "",
ConsumedUnits: 1,
Expand Down Expand Up @@ -117,7 +117,7 @@ func TestDBaaS_aggregatedDBaaS(t *testing.T) {

for name, tc := range tests {
t.Run(name, func(t *testing.T) {
ds, _ := NewDBaaS(nil, nil, nil, 1, "1234", "c-test1", map[string]string{})
ds, _ := NewDBaaS(nil, nil, nil, 1, "1234", "c-test1", "", map[string]string{})
aggregatedOdooRecords, err := ds.AggregateDBaaS(ctx, tc.exoscaleDBaaS, tc.dbaasDetails)
assert.NoError(t, err)
assert.Equal(t, tc.expectedAggregatedOdooRecords, aggregatedOdooRecords)
Expand Down
8 changes: 5 additions & 3 deletions pkg/exoscale/objectstorage.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ type ObjectStorage struct {
controlApiClient k8s.Client
salesOrder string
clusterId string
cloudZone string
uomMapping map[string]string
}

Expand All @@ -35,13 +36,14 @@ type BucketDetail struct {
}

// NewObjectStorage creates an ObjectStorage with the initial setup
func NewObjectStorage(exoscaleClient *egoscale.Client, k8sClient k8s.Client, controlApiClient k8s.Client, salesOrder, clusterId string, uomMapping map[string]string) (*ObjectStorage, error) {
func NewObjectStorage(exoscaleClient *egoscale.Client, k8sClient k8s.Client, controlApiClient k8s.Client, salesOrder, clusterId string, cloudZone string, uomMapping map[string]string) (*ObjectStorage, error) {
return &ObjectStorage{
k8sClient: k8sClient,
exoscaleClient: exoscaleClient,
controlApiClient: controlApiClient,
salesOrder: salesOrder,
clusterId: clusterId,
cloudZone: cloudZone,
uomMapping: uomMapping,
}, nil
}
Expand Down Expand Up @@ -110,11 +112,11 @@ func (o *ObjectStorage) getOdooMeteredBillingRecords(ctx context.Context, sosBuc
return nil, err
}

itemGroup := fmt.Sprintf("APPUiO Managed - Zone: %s / Namespace: %s", o.clusterId, bucketDetail.Namespace)
itemGroup := fmt.Sprintf("APPUiO Managed - Cluster: %s / Namespace: %s", o.clusterId, bucketDetail.Namespace)
instanceId := fmt.Sprintf("%s/%s", bucketDetail.Zone, bucketDetail.BucketName)
salesOrder := o.salesOrder
if salesOrder == "" {
itemGroup = fmt.Sprintf("APPUiO Cloud - Zone: %s / Namespace: %s", o.clusterId, bucketDetail.Namespace)
itemGroup = fmt.Sprintf("APPUiO Cloud - Zone: %s / Namespace: %s", o.cloudZone, bucketDetail.Namespace)
salesOrder, err = controlAPI.GetSalesOrder(ctx, o.controlApiClient, bucketDetail.Organization)
if err != nil {
logger.Error(err, "unable to sync bucket", "namespace", bucketDetail.Namespace)
Expand Down

0 comments on commit 6c9af3a

Please sign in to comment.