Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reconstruct status of project, if it already exists #73

Merged
merged 2 commits into from
Apr 1, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
16 changes: 14 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 All @@ -35,6 +36,7 @@ const (
// TODO: Check if this is required in our case
// defaultRequeueTime = 60 * time.Second
ProjectFinalizer string = "jiraservicedesk.stakater.com/project"
ProjectAlreadyExistsErr string = "A project with that name already exists."
)

// ProjectReconciler reconciles a Project object
Expand Down Expand Up @@ -99,7 +101,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 +135,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(), ProjectAlreadyExistsErr) {
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