From af028f143f486a9c7d3c8174b3201099a9e8397d Mon Sep 17 00:00:00 2001 From: QuentinBisson Date: Tue, 10 Dec 2024 17:25:52 +0100 Subject: [PATCH] remove useless pointer --- .../grafanaorganization_controller.go | 19 ++++++--------- pkg/grafana/grafana.go | 23 ++++++++++--------- 2 files changed, 19 insertions(+), 23 deletions(-) diff --git a/internal/controller/grafanaorganization_controller.go b/internal/controller/grafanaorganization_controller.go index ffb7a252..67bebcfa 100644 --- a/internal/controller/grafanaorganization_controller.go +++ b/internal/controller/grafanaorganization_controller.go @@ -210,7 +210,7 @@ func (r GrafanaOrganizationReconciler) configureSharedOrg(ctx context.Context) e sharedOrg := grafana.SharedOrg logger.Info("configuring shared organization") - if _, err := grafana.UpdateOrganization(ctx, r.GrafanaAPI, sharedOrg); err != nil { + if err := grafana.UpdateOrganization(ctx, r.GrafanaAPI, &sharedOrg); err != nil { logger.Error(err, "failed to rename shared org") return errors.WithStack(err) } @@ -224,13 +224,13 @@ func (r GrafanaOrganizationReconciler) configureSharedOrg(ctx context.Context) e return nil } -func newOrganization(grafanaOrganization *v1alpha1.GrafanaOrganization) *grafana.Organization { +func newOrganization(grafanaOrganization *v1alpha1.GrafanaOrganization) grafana.Organization { tenantIDs := make([]string, len(grafanaOrganization.Spec.Tenants)) for i, tenant := range grafanaOrganization.Spec.Tenants { tenantIDs[i] = string(tenant) } - return &grafana.Organization{ + return grafana.Organization{ ID: grafanaOrganization.Status.OrgID, Name: grafanaOrganization.Spec.DisplayName, TenantIDs: tenantIDs, @@ -241,12 +241,11 @@ func (r GrafanaOrganizationReconciler) configureOrganization(ctx context.Context logger := log.FromContext(ctx) // Create or update organization in Grafana var organization = newOrganization(grafanaOrganization) - if organization.ID == 0 { // if the CR doesn't have an orgID, create the organization in Grafana - organization, err = grafana.CreateOrganization(ctx, r.GrafanaAPI, *organization) + err = grafana.CreateOrganization(ctx, r.GrafanaAPI, &organization) } else { - organization, err = grafana.UpdateOrganization(ctx, r.GrafanaAPI, *organization) + err = grafana.UpdateOrganization(ctx, r.GrafanaAPI, &organization) } if err != nil { @@ -276,7 +275,7 @@ func (r GrafanaOrganizationReconciler) configureDatasources(ctx context.Context, // Create or update organization in Grafana var organization = newOrganization(grafanaOrganization) - datasources, err := grafana.ConfigureDefaultDatasources(ctx, r.GrafanaAPI, *organization) + datasources, err := grafana.ConfigureDefaultDatasources(ctx, r.GrafanaAPI, organization) if err != nil { return errors.WithStack(err) } @@ -311,11 +310,7 @@ func (r GrafanaOrganizationReconciler) reconcileDelete(ctx context.Context, graf } // Delete organization in Grafana - var organization = grafana.Organization{ - ID: grafanaOrganization.Status.OrgID, - Name: grafanaOrganization.Spec.DisplayName, - TenantID: grafanaOrganization.Name, - } + var organization = newOrganization(grafanaOrganization) // Delete organization in Grafana if it exists if grafanaOrganization.Status.OrgID > 0 { diff --git a/pkg/grafana/grafana.go b/pkg/grafana/grafana.go index cfd1a2f2..c1a39213 100644 --- a/pkg/grafana/grafana.go +++ b/pkg/grafana/grafana.go @@ -69,13 +69,13 @@ var defaultDatasources = []Datasource{ }, } -func CreateOrganization(ctx context.Context, grafanaAPI *client.GrafanaHTTPAPI, organization Organization) (*Organization, error) { +func CreateOrganization(ctx context.Context, grafanaAPI *client.GrafanaHTTPAPI, organization *Organization) error { logger := log.FromContext(ctx) logger.Info("creating organization") err := assertNameIsAvailable(ctx, grafanaAPI, organization) if err != nil { - return nil, errors.WithStack(err) + return errors.WithStack(err) } createdOrg, err := grafanaAPI.Orgs.CreateOrg(&models.CreateOrgCommand{ @@ -83,15 +83,15 @@ func CreateOrganization(ctx context.Context, grafanaAPI *client.GrafanaHTTPAPI, }) if err != nil { logger.Error(err, "failed to create organization") - return nil, errors.WithStack(err) + return errors.WithStack(err) } logger.Info("created organization") organization.ID = *createdOrg.Payload.OrgID - return &organization, nil + return nil } -func UpdateOrganization(ctx context.Context, grafanaAPI *client.GrafanaHTTPAPI, organization Organization) (*Organization, error) { +func UpdateOrganization(ctx context.Context, grafanaAPI *client.GrafanaHTTPAPI, organization *Organization) error { logger := log.FromContext(ctx) logger.Info("updating organization") @@ -103,18 +103,18 @@ func UpdateOrganization(ctx context.Context, grafanaAPI *client.GrafanaHTTPAPI, return CreateOrganization(ctx, grafanaAPI, organization) } logger.Error(err, fmt.Sprintf("failed to find organization with ID: %d", organization.ID)) - return nil, errors.WithStack(err) + return errors.WithStack(err) } // If both name matches, there is nothing to do. if found.Name == organization.Name { logger.Info("the organization already exists in Grafana and does not need to be updated.") - return &organization, nil + return nil } err = assertNameIsAvailable(ctx, grafanaAPI, organization) if err != nil { - return nil, errors.WithStack(err) + return errors.WithStack(err) } // if the name of the CR is different from the name of the org in Grafana, update the name of the org in Grafana using the CR's display name. @@ -123,12 +123,12 @@ func UpdateOrganization(ctx context.Context, grafanaAPI *client.GrafanaHTTPAPI, }) if err != nil { logger.Error(err, "failed to update organization name") - return nil, errors.WithStack(err) + return errors.WithStack(err) } logger.Info("updated organization") - return &organization, nil + return nil } func DeleteOrganization(ctx context.Context, grafanaAPI *client.GrafanaHTTPAPI, organization Organization) error { @@ -158,6 +158,7 @@ func DeleteOrganization(ctx context.Context, grafanaAPI *client.GrafanaHTTPAPI, func ConfigureDefaultDatasources(ctx context.Context, grafanaAPI *client.GrafanaHTTPAPI, organization Organization) ([]Datasource, error) { logger := log.FromContext(ctx) + // TODO using a serviceaccount later would be better as they are scoped to an organization var err error @@ -278,7 +279,7 @@ func isNotFound(err error) bool { } // assertNameIsAvailable is a helper function to check if the organization name is available in Grafana -func assertNameIsAvailable(ctx context.Context, grafanaAPI *client.GrafanaHTTPAPI, organization Organization) error { +func assertNameIsAvailable(ctx context.Context, grafanaAPI *client.GrafanaHTTPAPI, organization *Organization) error { logger := log.FromContext(ctx) found, err := findByName(grafanaAPI, organization.Name)