Skip to content

Commit

Permalink
remove useless pointer
Browse files Browse the repository at this point in the history
  • Loading branch information
QuentinBisson committed Dec 10, 2024
1 parent d9b3fa4 commit af028f1
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 23 deletions.
19 changes: 7 additions & 12 deletions internal/controller/grafanaorganization_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand All @@ -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,
Expand All @@ -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 {
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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 {
Expand Down
23 changes: 12 additions & 11 deletions pkg/grafana/grafana.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,29 +69,29 @@ 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{
Name: organization.Name,
})
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")
Expand All @@ -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.
Expand All @@ -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 {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit af028f1

Please sign in to comment.