From 56b47ae62ae4d809edac463641523ffd00c4d18e Mon Sep 17 00:00:00 2001 From: Waleed Malik Date: Thu, 1 Apr 2021 13:28:01 +0500 Subject: [PATCH] Reconstruct status of project, if it already exists --- controllers/customer_controller.go | 1 + controllers/project_controller.go | 15 +++++++++++++-- pkg/jiraservicedesk/client/client.go | 2 +- pkg/jiraservicedesk/client/client_project.go | 2 +- pkg/jiraservicedesk/client/client_project_test.go | 4 ++-- 5 files changed, 18 insertions(+), 6 deletions(-) diff --git a/controllers/customer_controller.go b/controllers/customer_controller.go index 6487b4e..9be70d6 100644 --- a/controllers/customer_controller.go +++ b/controllers/customer_controller.go @@ -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) diff --git a/controllers/project_controller.go b/controllers/project_controller.go index 204535c..a52912e 100644 --- a/controllers/project_controller.go +++ b/controllers/project_controller.go @@ -18,6 +18,7 @@ package controllers import ( "context" + "strings" "github.com/go-logr/logr" "k8s.io/apimachinery/pkg/api/errors" @@ -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) } @@ -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) } diff --git a/pkg/jiraservicedesk/client/client.go b/pkg/jiraservicedesk/client/client.go index cb097b8..0212d45 100644 --- a/pkg/jiraservicedesk/client/client.go +++ b/pkg/jiraservicedesk/client/client.go @@ -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) diff --git a/pkg/jiraservicedesk/client/client_project.go b/pkg/jiraservicedesk/client/client_project.go index 947ef12..0ddeec0 100644 --- a/pkg/jiraservicedesk/client/client_project.go +++ b/pkg/jiraservicedesk/client/client_project.go @@ -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) diff --git a/pkg/jiraservicedesk/client/client_project_test.go b/pkg/jiraservicedesk/client/client_project_test.go index 8fafa63..010d5b9 100644 --- a/pkg/jiraservicedesk/client/client_project_test.go +++ b/pkg/jiraservicedesk/client/client_project_test.go @@ -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) @@ -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)