Skip to content

Commit

Permalink
Reconstruct status of project, if it already exists
Browse files Browse the repository at this point in the history
  • Loading branch information
ahmedwaleedmalik committed Apr 1, 2021
1 parent 9b6cffc commit 56b47ae
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 6 deletions.
1 change: 1 addition & 0 deletions controllers/customer_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ func (r *CustomerReconciler) Reconcile(req ctrl.Request) (ctrl.Result, error) {
}
}

// If CustomerId exists in status, then it's an update request
if len(instance.Status.CustomerId) > 0 {
// Get the customer from Jira Service Desk
existingCustomer, err := r.JiraServiceDeskClient.GetCustomerById(instance.Status.CustomerId)
Expand Down
15 changes: 13 additions & 2 deletions controllers/project_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 Down Expand Up @@ -99,7 +100,7 @@ func (r *ProjectReconciler) Reconcile(req ctrl.Request) (ctrl.Result, error) {

// Check if the Project already exists
if len(instance.Status.ID) > 0 {
existingProject, err := r.JiraServiceDeskClient.GetProjectById(instance.Status.ID)
existingProject, err := r.JiraServiceDeskClient.GetProjectByIdentifier(instance.Status.ID)
if err != nil {
return reconcilerUtil.ManageError(r.Client, instance, err, false)
}
Expand Down Expand Up @@ -133,7 +134,17 @@ func (r *ProjectReconciler) handleCreate(req ctrl.Request, instance *jiraservice

project := r.JiraServiceDeskClient.GetProjectFromProjectCR(instance)
projectId, err := r.JiraServiceDeskClient.CreateProject(project)
if err != nil {

// If project already exists then reconstruct status
if err != nil && strings.Contains(err.Error(), "A project with that name already exists.") {
existingProject, err := r.JiraServiceDeskClient.GetProjectByIdentifier(instance.Spec.Key)
if err != nil {
return reconcilerUtil.ManageError(r.Client, instance, err, false)
}
log.Info("Successfully reconstructed status for Jira Service Desk Project " + instance.Spec.Name)

projectId = existingProject.Id
} else if err != nil {
return reconcilerUtil.ManageError(r.Client, instance, err, false)
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/jiraservicedesk/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ var Log = logf.Log.WithName("jiraServiceDeskClient")

type Client interface {
// Methods for Project
GetProjectById(id string) (Project, error)
GetProjectByIdentifier(identifier string) (Project, error)
GetProjectFromProjectCR(project *jiraservicedeskv1alpha1.Project) Project
GetProjectCRFromProject(project Project) jiraservicedeskv1alpha1.Project
CreateProject(project Project) (string, error)
Expand Down
2 changes: 1 addition & 1 deletion pkg/jiraservicedesk/client/client_project.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ type CustomerAccessRequestBody struct {
serviceDeskPublicSignup bool
}

func (c *jiraServiceDeskClient) GetProjectById(id string) (Project, error) {
func (c *jiraServiceDeskClient) GetProjectByIdentifier(id string) (Project, error) {
var project Project

request, err := c.newRequest("GET", EndpointApiVersion3Project+"/"+id, nil, false)
Expand Down
4 changes: 2 additions & 2 deletions pkg/jiraservicedesk/client/client_project_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func TestJiraService_GetProject_shouldGetProject_whenValidProjectIdIsGiven(t *te
JSON(mockData.GetProjectByIdResponseJSON)

jiraClient := NewClient("", mockData.BaseURL, "")
project, err := jiraClient.GetProjectById("/" + mockData.ProjectID)
project, err := jiraClient.GetProjectByIdentifier("/" + mockData.ProjectID)

st.Expect(t, project.Description, mockData.GetProjectByIdExpectedResponse.Description)
st.Expect(t, project.Name, mockData.GetProjectByIdExpectedResponse.Name)
Expand All @@ -41,7 +41,7 @@ func TestJiraService_GetProject_shouldNotGetProject_whenInValidProjectIdIsGiven(
Reply(404)

jiraClient := NewClient("", mockData.BaseURL, "")
_, err := jiraClient.GetProjectById("/" + mockData.ProjectID)
_, err := jiraClient.GetProjectByIdentifier("/" + mockData.ProjectID)

st.Expect(t, err, errors.New(mockData.GetProjectFailedErrorMsg))
st.Expect(t, gock.IsDone(), true)
Expand Down

0 comments on commit 56b47ae

Please sign in to comment.