Skip to content

Commit 9edba7f

Browse files
Fixed agent create crash (#6570)
* Fixed agent create crash * Fixed agent delete logic
1 parent aa73497 commit 9edba7f

File tree

2 files changed

+91
-14
lines changed

2 files changed

+91
-14
lines changed

ibm/service/schematics/resource_ibm_schematics_agent.go

Lines changed: 43 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -586,7 +586,14 @@ func resourceIbmSchematicsAgentCreate(context context.Context, d *schema.Resourc
586586
if _, ok := d.GetOk("agent_metadata"); ok {
587587
var agentMetadata []schematicsv1.AgentMetadataInfo
588588
for _, e := range d.Get("agent_metadata").([]interface{}) {
589-
value := e.(map[string]interface{})
589+
if e == nil {
590+
continue
591+
}
592+
593+
value, ok := e.(map[string]interface{})
594+
if !ok {
595+
continue
596+
}
590597
agentMetadataItem, err := resourceIbmSchematicsAgentMapToAgentMetadataInfo(value)
591598
if err != nil {
592599
tfErr := flex.TerraformErrorf(err, fmt.Sprintf("resourceIbmSchematicsAgentCreate failed: %s", err.Error()), "ibm_schematics_agent", "create")
@@ -1061,24 +1068,48 @@ func resourceIbmSchematicsAgentDelete(context context.Context, d *schema.Resourc
10611068
deleteAgentDataOptions.Headers = ff
10621069

10631070
deleteAgentDataOptions.SetAgentID(d.Id())
1071+
// get the agent data and check if deploy resources exist and then call this
10641072

1065-
// first try destroying resources associated with agent deploy and then delete the agent
1066-
1067-
deleteAgentResourcesOptions := &schematicsv1.DeleteAgentResourcesOptions{}
1068-
deleteAgentResourcesOptions.Headers = ff
1073+
getAgentDataOptions := &schematicsv1.GetAgentDataOptions{
1074+
Profile: core.StringPtr("detailed"),
1075+
}
10691076

1070-
deleteAgentResourcesOptions.SetAgentID(d.Id())
1071-
deleteAgentResourcesOptions.SetRefreshToken(iamRefreshToken)
1077+
getAgentDataOptions.SetAgentID(d.Id())
10721078

1073-
response, err := schematicsClient.DeleteAgentResourcesWithContext(context, deleteAgentResourcesOptions)
1079+
agentData, response, err := schematicsClient.GetAgentDataWithContext(context, getAgentDataOptions)
10741080
if err != nil {
1075-
tfErr := flex.TerraformErrorf(err, fmt.Sprintf("resourceIbmSchematicsAgentDelete DeleteAgentResourcesWithContext failed with error: %s and response:\n%s", err, response), "ibm_schematics_agent", "delete")
1081+
if response != nil && response.StatusCode == 404 {
1082+
d.SetId("")
1083+
return nil
1084+
}
1085+
1086+
tfErr := flex.TerraformErrorf(err, fmt.Sprintf("resourceIbmSchematicsAgentUpdate GetAgentDataWithContext failed with error: %s and response:\n%s", err, response), "ibm_schematics_agent", "update")
10761087
log.Printf("[DEBUG]\n%s", tfErr.GetDebugMessage())
1077-
} else {
1078-
_, err = isWaitForAgentDestroyResources(context, schematicsClient, *deleteAgentResourcesOptions.AgentID, d.Timeout(schema.TimeoutDelete))
1079-
if err != nil {
1088+
return tfErr.GetDiag()
1089+
}
1090+
1091+
// first try destroying resources associated with agent deploy and then delete the agent
1092+
if agentData.RecentDeployJob != nil {
1093+
if agentData.RecentDeployJob.JobID != nil {
1094+
// first try destroying resources associated with agent deploy and then delete the agent
1095+
deleteAgentResourcesOptions := &schematicsv1.DeleteAgentResourcesOptions{}
1096+
deleteAgentResourcesOptions.Headers = ff
1097+
1098+
deleteAgentResourcesOptions.SetAgentID(d.Id())
1099+
deleteAgentResourcesOptions.SetRefreshToken(iamRefreshToken)
10801100

1101+
response, err := schematicsClient.DeleteAgentResourcesWithContext(context, deleteAgentResourcesOptions)
1102+
if err != nil {
1103+
tfErr := flex.TerraformErrorf(err, fmt.Sprintf("resourceIbmSchematicsAgentDelete DeleteAgentResourcesWithContext failed with error: %s and response:\n%s", err, response), "ibm_schematics_agent", "delete")
1104+
log.Printf("[DEBUG]\n%s", tfErr.GetDebugMessage())
1105+
} else {
1106+
_, err = isWaitForAgentDestroyResources(context, schematicsClient, *deleteAgentResourcesOptions.AgentID, d.Timeout(schema.TimeoutDelete))
1107+
if err != nil {
1108+
1109+
}
1110+
}
10811111
}
1112+
10821113
}
10831114

10841115
// After deploy associated resources are destroyed, now attempt to delete the agent

ibm/service/schematics/resource_ibm_schematics_agent_test.go

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,11 +58,11 @@ func TestAccIbmSchematicsAgentBasic(t *testing.T) {
5858
func TestAccIbmSchematicsAgentAllArgs(t *testing.T) {
5959
var conf schematicsv1.AgentData
6060
name := fmt.Sprintf("tf_name_%d", acctest.RandIntRange(10, 100))
61-
version := "1.1.0"
61+
version := "1.4.0"
6262
schematicsLocation := "us-south"
6363
agentLocation := "eu-de"
6464
nameUpdate := fmt.Sprintf("tf_name_%d", acctest.RandIntRange(10, 100))
65-
versionUpdate := "1.2.0"
65+
versionUpdate := "1.5.0"
6666
schematicsLocationUpdate := "us-east"
6767
agentLocationUpdate := "eu-gb"
6868
description := fmt.Sprintf("tf_description_%d", acctest.RandIntRange(10, 100))
@@ -94,6 +94,19 @@ func TestAccIbmSchematicsAgentAllArgs(t *testing.T) {
9494
resource.TestCheckResourceAttr("ibm_schematics_agent.schematics_agent_instance", "description", descriptionUpdate),
9595
),
9696
},
97+
resource.TestStep{
98+
Config: testAccIbmSchematicsAgentConfig_withMetadata(name, version, schematicsLocation, agentLocation),
99+
Check: resource.ComposeTestCheckFunc(
100+
testAccCheckIbmSchematicsAgentExists("ibm_schematics_agent.schematics_agent_instance", conf),
101+
resource.TestCheckResourceAttr("ibm_schematics_agent.schematics_agent_instance", "name", name),
102+
resource.TestCheckResourceAttr("ibm_schematics_agent.schematics_agent_instance", "version", version),
103+
resource.TestCheckResourceAttr("ibm_schematics_agent.schematics_agent_instance", "schematics_location", schematicsLocation),
104+
resource.TestCheckResourceAttr("ibm_schematics_agent.schematics_agent_instance", "agent_location", agentLocation),
105+
resource.TestCheckResourceAttr("ibm_schematics_agent.schematics_agent_instance", "agent_metadata.#", "2"),
106+
resource.TestCheckResourceAttr("ibm_schematics_agent.schematics_agent_instance", "agent_metadata.0.name", "purpose"),
107+
resource.TestCheckResourceAttr("ibm_schematics_agent.schematics_agent_instance", "agent_metadata.0.value.0", "terraform"),
108+
),
109+
},
97110
resource.TestStep{
98111
ResourceName: "ibm_schematics_agent.schematics_agent_instance",
99112
ImportState: true,
@@ -151,6 +164,39 @@ func testAccCheckIbmSchematicsAgentConfig(name string, version string, schematic
151164
`, name, version, schematicsLocation, agentLocation, description)
152165
}
153166

167+
func testAccIbmSchematicsAgentConfig_withMetadata(name, version, schematicsLocation, agentLocation string) string {
168+
return fmt.Sprintf(`
169+
resource "ibm_schematics_agent" "schematics_agent_instance" {
170+
name = "%s"
171+
resource_group = "Default"
172+
version = "%s"
173+
schematics_location = "%s"
174+
agent_location = "%s"
175+
tags = ["test-tag"]
176+
agent_infrastructure {
177+
infra_type = "ibm_kubernetes"
178+
cluster_id = "cluster_id"
179+
cluster_resource_group = "cluster_resource_group"
180+
cos_instance_name = "cos_instance_name"
181+
cos_bucket_name = "cos_bucket_name"
182+
cos_bucket_region = "cos_bucket_region"
183+
}
184+
185+
// This block triggers the loop at line 589
186+
agent_metadata {
187+
name = "purpose"
188+
value = ["terraform"]
189+
}
190+
// Adding a second block to ensure the loop handles multiple items
191+
agent_metadata {
192+
name = "purpose"
193+
value = ["testing"]
194+
}
195+
196+
}
197+
`, name, version, schematicsLocation, agentLocation)
198+
}
199+
154200
func testAccCheckIbmSchematicsAgentExists(n string, obj schematicsv1.AgentData) resource.TestCheckFunc {
155201

156202
return func(s *terraform.State) error {

0 commit comments

Comments
 (0)