Skip to content

Commit

Permalink
feat: provider | agent delete (#58)
Browse files Browse the repository at this point in the history
  • Loading branch information
1riatsila1 authored Nov 1, 2024
1 parent f57f223 commit f75beaa
Show file tree
Hide file tree
Showing 4 changed files with 115 additions and 4 deletions.
11 changes: 9 additions & 2 deletions pkg/config-api-provider/provider/resources/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -215,8 +215,15 @@ func (r *agentResource) Delete(
return
}

// Delete existing agent using client here
// err := r.client.DeleteOrder(state.ID.ValueString())
request := r.client.ConfigurationAPI.AgentsDelete(ctx, state.ID.ValueString())

_, response, err := util.RetryFor429(request.Execute)
errorPresent, errorDetail := util.RaiseForStatus(response, err)

if errorPresent {
resp.Diagnostics.AddError(util.GenerateErrorSummary("delete", "uxi_agent"), errorDetail)
return
}
}

func (r *agentResource) ImportState(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,8 @@ func TestAgentGroupAssignmentResource(t *testing.T) {
)
util.MockDeleteGroup("group_uid", 1)
util.MockDeleteGroup("group_uid_2", 1)
util.MockDeleteAgent("agent_uid", 1)
util.MockDeleteAgent("agent_uid_2", 1)
},
Config: provider.ProviderConfig,
},
Expand Down
98 changes: 96 additions & 2 deletions pkg/config-api-provider/test/resources/agent_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,17 @@ func TestAgentResource(t *testing.T) {
resource.TestCheckResourceAttr("uxi_agent.my_agent", "pcap_mode", "light_2"),
),
},
// Delete testing automatically occurs in TestCase
// Delete testing
{
PreConfig: func() {
util.MockGetAgent("uid", util.GeneratePaginatedResponse(
[]map[string]interface{}{util.GenerateAgentResponseModel("uid", "")}),
1,
)
util.MockDeleteAgent("uid", 1)
},
Config: provider.ProviderConfig,
},
},
})

Expand Down Expand Up @@ -160,7 +170,27 @@ func TestAgentResource429Handling(t *testing.T) {
},
),
},
// Deletion occurs automatically
// Delete testing
{
PreConfig: func() {
util.MockGetAgent("uid", util.GeneratePaginatedResponse(
[]map[string]interface{}{util.GenerateAgentResponseModel("uid", "")}),
1,
)
request429 = gock.New("https://test.api.capenetworks.com").
Delete("/networking-uxi/v1alpha1/agents/uid").
Reply(429).
SetHeaders(util.RateLimitingHeaders)
util.MockDeleteAgent("uid", 1)
},
Config: provider.ProviderConfig,
Check: resource.ComposeAggregateTestCheckFunc(
func(s *terraform.State) error {
st.Assert(t, request429.Mock.Request().Counter, 0)
return nil
},
),
},
},
})

Expand Down Expand Up @@ -230,6 +260,70 @@ func TestAgentResourceHttpErrorHandling(t *testing.T) {

ExpectError: regexp.MustCompile(`Error: Cannot import non-existent remote object`),
},
// Actually importing an agent for testing purposes
{
PreConfig: func() {
util.MockGetAgent(
"uid",
util.GeneratePaginatedResponse(
[]map[string]interface{}{util.GenerateAgentResponseModel("uid", "")},
),
2,
)
},
Config: provider.ProviderConfig + `
resource "uxi_agent" "my_agent" {
name = "name"
notes = "notes"
pcap_mode = "light"
}
import {
to = uxi_agent.my_agent
id = "uid"
}`,

Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr("uxi_agent.my_agent", "id", "uid"),
),
},
// Delete 4xx
{
PreConfig: func() {
// existing agent
util.MockGetAgent("uid", util.GeneratePaginatedResponse(
[]map[string]interface{}{util.GenerateAgentResponseModel("uid", "")}),
1,
)
// delete agent - with error
gock.New("https://test.api.capenetworks.com").
Delete("/networking-uxi/v1alpha1/agents/uid").
Reply(422).
JSON(map[string]interface{}{
"httpStatusCode": 422,
"errorCode": "HPE_GL_NETWORKING_UXI_HARDWARE_SENSOR_DELETION_FORBIDDEN",
"message": "Cant delete sensor - hardware sensor deletion is forbidden",
"debugId": "12312-123123-123123-1231212",
})
},
Config: provider.ProviderConfig,
ExpectError: regexp.MustCompile(
`(?s)Cant delete sensor - hardware sensor deletion is forbidden\s*DebugID: 12312-123123-123123-1231212`,
),
},
// Actually delete group for cleanup reasons
{
PreConfig: func() {
// existing group
util.MockGetAgent("uid", util.GeneratePaginatedResponse(
[]map[string]interface{}{util.GenerateAgentResponseModel("uid", "")}),
1,
)
// delete group
util.MockDeleteAgent("uid", 1)
},
Config: provider.ProviderConfig,
},
},
})

Expand Down
8 changes: 8 additions & 0 deletions pkg/config-api-provider/test/util/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,14 @@ func MockGetAgent(uid string, response map[string]interface{}, times int) {
JSON(response)
}

func MockDeleteAgent(uid string, times int) {
gock.New("https://test.api.capenetworks.com").
Delete("/networking-uxi/v1alpha1/agents/"+uid).
MatchHeader("Authorization", "mock_token").
Times(times).
Reply(204)
}

func MockPostGroup(request map[string]interface{}, response map[string]interface{}, times int) {
gock.New("https://test.api.capenetworks.com").
Post("/networking-uxi/v1alpha1/groups").
Expand Down

0 comments on commit f75beaa

Please sign in to comment.