Skip to content

Commit

Permalink
Fix an issue when the Module cannot get Workspace by ID (#77)
Browse files Browse the repository at this point in the history
  • Loading branch information
arybolovlev authored Jan 31, 2023
1 parent 2403013 commit 4791d86
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 5 deletions.
58 changes: 54 additions & 4 deletions controllers/module_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,6 @@ var _ = Describe("Module controller", Ordered, func() {
Sensitive: true,
},
},
Workspace: &appv1alpha2.ModuleWorkspace{
Name: workspace,
},
},
Status: appv1alpha2.ModuleStatus{},
}
Expand All @@ -96,7 +93,59 @@ var _ = Describe("Module controller", Ordered, func() {
})

Context("Module controller", func() {
It("can create, run and destroy module", func() {
It("can create, run and destroy module, ref to Workspace by Name", func() {
// Create a new TFC Workspace
ws, err := tfClient.Workspaces.Create(ctx, organization, tfc.WorkspaceCreateOptions{
Name: &workspace,
AutoApply: tfc.Bool(true),
})
Expect(err).Should(Succeed())
Expect(ws).ShouldNot(BeNil())

// Create TFC Workspace variables
_, err = tfClient.Variables.Create(ctx, ws.ID, tfc.VariableCreateOptions{
Key: tfc.String("string_length"),
Value: tfc.String("512"),
HCL: tfc.Bool(true),
Category: tfc.Category(tfc.CategoryTerraform),
})
Expect(err).Should(Succeed())

instance.Spec.Workspace = &appv1alpha2.ModuleWorkspace{Name: ws.Name}
// Create a new Module
Expect(k8sClient.Create(ctx, instance)).Should(Succeed())

// Make sure a new module is created and executed
Eventually(func() bool {
Expect(k8sClient.Get(ctx, namespacedName, instance)).Should(Succeed())
return instance.Status.ObservedGeneration == instance.Generation
}).Should(BeTrue())

Eventually(func() bool {
Expect(k8sClient.Get(ctx, namespacedName, instance)).Should(Succeed())
if instance.Status.ConfigurationVersion == nil {
return false
}
return instance.Status.ConfigurationVersion.Status == string(tfc.ConfigurationUploaded) ||
// If the Configuration Version upload is errored then exit from the Eventually and validate it after
instance.Status.ConfigurationVersion.Status == string(tfc.ConfigurationErrored)
}).Should(BeTrue())
Expect(k8sClient.Get(ctx, namespacedName, instance)).Should(Succeed())
Expect(instance.Status.ConfigurationVersion.Status).NotTo(BeEquivalentTo(string(tfc.ConfigurationErrored)))

Eventually(func() bool {
Expect(k8sClient.Get(ctx, namespacedName, instance)).Should(Succeed())
if instance.Status.Run == nil {
return false
}
return instance.Status.Run.Status == string(tfc.RunApplied) ||
// If the Run execution is errored then exit from the Eventually and validate it after
instance.Status.Run.Status == string(tfc.RunErrored)
}).Should(BeTrue())
Expect(k8sClient.Get(ctx, namespacedName, instance)).Should(Succeed())
Expect(instance.Status.Run.Status).NotTo(BeEquivalentTo(string(tfc.RunErrored)))
})
It("can create, run and destroy module, ref to Workspace by ID", func() {
// Create a new TFC Workspace
ws, err := tfClient.Workspaces.Create(ctx, organization, tfc.WorkspaceCreateOptions{
Name: &workspace,
Expand All @@ -114,6 +163,7 @@ var _ = Describe("Module controller", Ordered, func() {
})
Expect(err).Should(Succeed())

instance.Spec.Workspace = &appv1alpha2.ModuleWorkspace{ID: ws.ID}
// Create a new Module
Expect(k8sClient.Create(ctx, instance)).Should(Succeed())

Expand Down
2 changes: 1 addition & 1 deletion controllers/module_controller_workspace.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ func (r *ModuleReconciler) getWorkspaceByName(ctx context.Context, m *moduleInst
}

func (r *ModuleReconciler) getWorkspaceByID(ctx context.Context, m *moduleInstance) (*tfc.Workspace, error) {
return m.tfClient.Client.Workspaces.Read(ctx, m.instance.Spec.Organization, m.instance.Spec.Workspace.ID)
return m.tfClient.Client.Workspaces.ReadByID(ctx, m.instance.Spec.Workspace.ID)
}

func (r *ModuleReconciler) getWorkspace(ctx context.Context, m *moduleInstance) (*tfc.Workspace, error) {
Expand Down

0 comments on commit 4791d86

Please sign in to comment.