Skip to content

Commit

Permalink
Reconstruct status of customer
Browse files Browse the repository at this point in the history
  • Loading branch information
ahmedwaleedmalik committed Apr 1, 2021
1 parent 9b6cffc commit 5d6ee8d
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 9 deletions.
26 changes: 17 additions & 9 deletions controllers/customer_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package controllers

import (
"context"
"strings"

"github.com/go-logr/logr"
"k8s.io/apimachinery/pkg/api/errors"
Expand All @@ -32,7 +33,8 @@ import (
)

const (
CustomerFinalizer string = "jiraservicedesk.stakater.com/customer"
CustomerFinalizer string = "jiraservicedesk.stakater.com/customer"
CustomerAlreadyExistsErr string = "An account already exists for this email"
)

// CustomerReconciler reconciles a Customer object
Expand Down Expand Up @@ -175,25 +177,31 @@ func (r *CustomerReconciler) handleCreate(req ctrl.Request, instance *jiraservic
log := r.Log.WithValues("customer", req.NamespacedName)

log.Info("Creating Jira Service Desk Customer: " + instance.Spec.Name)
var customerID string
var err error

// If legacy Customer flag is true than create a legacy customer, else create a normal customer
if instance.Spec.LegacyCustomer {
customerID, err := r.JiraServiceDeskClient.CreateLegacyCustomer(instance.Spec.Email, instance.Spec.Projects[0])
if err != nil {
return reconcilerUtil.ManageError(r.Client, instance, err, false)
}

instance.Status.CustomerId = customerID
customerID, err = r.JiraServiceDeskClient.CreateLegacyCustomer(instance.Spec.Email, instance.Spec.Projects[0])
} else {
customer := r.JiraServiceDeskClient.GetCustomerFromCustomerCRForCreateCustomer(instance)
customerID, err := r.JiraServiceDeskClient.CreateCustomer(customer)
customerID, err = r.JiraServiceDeskClient.CreateCustomer(customer)
}

// If customer already exists, reconstruct status of the custom resource
if err != nil && strings.Contains(err.Error(), CustomerAlreadyExistsErr) {
existingCustomerID, err := r.JiraServiceDeskClient.GetCustomerIdByEmail(instance.Spec.Email)
if err != nil {
return reconcilerUtil.ManageError(r.Client, instance, err, false)
}
customerID = existingCustomerID

instance.Status.CustomerId = customerID
} else if err != nil {
return reconcilerUtil.ManageError(r.Client, instance, err, false)
}

instance.Status.CustomerId = customerID

log.Info("Successfully created Jira Service Desk Customer: " + instance.Spec.Name)

log.Info("Adding project associations for JSD Customer: " + instance.Spec.Name)
Expand Down
1 change: 1 addition & 0 deletions pkg/jiraservicedesk/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ type Client interface {
GetProjectForUpdateRequest(existingProject Project, newProject *jiraservicedeskv1alpha1.Project) Project
UpdateProjectAccessPermissions(status bool, key string) error
GetCustomerById(customerAccountId string) (Customer, error)
GetCustomerIdByEmail(emailAddress string) (string, error)
CreateCustomer(customer Customer) (string, error)
CreateLegacyCustomer(email string, projectKey string) (string, error)
IsCustomerUpdated(customer *jiraservicedeskv1alpha1.Customer, existingCustomer Customer) bool
Expand Down
34 changes: 34 additions & 0 deletions pkg/jiraservicedesk/client/client_customer.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ const (
EndpointUser = "/rest/api/3/user?accountId="
LegacyCustomerApiPath = "/rest/servicedesk/1/pages/people/customers/pagination/"
LegacyCustomerCreateEndpoint = "/invite"
SearchUserEndpoint = "/rest/api/3/user/search?query="
)

type Customer struct {
Expand Down Expand Up @@ -44,6 +45,8 @@ type CustomerGetResponse struct {
AccountType string `json:"accountType,omitempty"`
}

type CustomerGetByEmailResponse []CustomerGetResponse

type LegacyCustomerRequestBody struct {
Emails []string `json:"emails,omitempty"`
}
Expand Down Expand Up @@ -90,6 +93,37 @@ func (c *jiraServiceDeskClient) GetCustomerById(customerAccountId string) (Custo
return customer, err
}

// GetCustomerById gets a customer by ID from JSD
func (c *jiraServiceDeskClient) GetCustomerIdByEmail(emailAddress string) (string, error) {
request, err := c.newRequest("GET", SearchUserEndpoint+emailAddress, nil, false)
if err != nil {
return "", err
}

response, err := c.do(request)
if err != nil {
return "", err
}
defer response.Body.Close()

if response.StatusCode < 200 || response.StatusCode > 299 {
err := errors.New("Rest request to get customer failed with status: " + strconv.Itoa(response.StatusCode))
return "", err
}

var responseObject CustomerGetByEmailResponse
err = json.NewDecoder(response.Body).Decode(&responseObject)
if err != nil {
return "", err
}

if len(responseObject) > 0 {
return responseObject[0].AccountId, err
}

return "", err
}

// CreateCustomer create a new customer on JSD
func (c *jiraServiceDeskClient) CreateCustomer(customer Customer) (string, error) {
request, err := c.newRequest("POST", CreateCustomerApiPath, customer, false)
Expand Down

0 comments on commit 5d6ee8d

Please sign in to comment.