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

Add Host resource #5

Closed
wants to merge 6 commits into from
Closed
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
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

default: build

build:
build:
@echo "==> Building package..."
go build ./...

Expand Down
30 changes: 30 additions & 0 deletions examples/resources/host/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
terraform {
required_providers {
aap = {
source = "ansible/aap"
}
}
}

provider "aap" {
host = "https://localhost:8043"
username = "test"
password = "test"
insecure_skip_verify = true
}

resource "aap_host" "sample" {
inventory_id = 1
name = "tf_host"
variables = jsonencode(
{
"foo": "bar"
}
)
group_id = 2
disassociate_group = true
}

output "host" {
value = aap_host.sample
}
55 changes: 4 additions & 51 deletions internal/provider/group_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,19 +174,7 @@ func (r GroupResource) CreateGroup(data GroupResourceModelInterface) diag.Diagno
}

resp, body, err := r.client.doRequest(http.MethodPost, "/api/v2/groups/", req_data)
if err != nil {
diags.AddError("Body JSON Marshal Error", err.Error())
return diags
}
if resp == nil {
diags.AddError("Http response Error", "no http response from server")
return diags
}
if resp.StatusCode != http.StatusCreated {
diags.AddError("Unexpected Http Status code",
fmt.Sprintf("expected (%d) got (%s)", http.StatusCreated, resp.Status))
return diags
}
diags.Append(IsResponseValid(resp, err, http.StatusCreated)...)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This function no longer stops execution when a fatal error was reached. You should check if the diagnostics object has errors before continuing: https://developer.hashicorp.com/terraform/plugin/framework/diagnostics#haserror

err = data.ParseHttpResponse(body)
if err != nil {
diags.AddError("error while parsing the json response: ", err.Error())
Expand Down Expand Up @@ -218,19 +206,7 @@ func (r GroupResource) DeleteGroup(data GroupResourceModelInterface) diag.Diagno
var diags diag.Diagnostics

resp, _, err := r.client.doRequest(http.MethodDelete, data.GetURL(), nil)
if err != nil {
diags.AddError("Body JSON Marshal Error", err.Error())
return diags
}
if resp == nil {
diags.AddError("Http response Error", "no http response from server")
return diags
}
if resp.StatusCode != http.StatusNoContent {
diags.AddError("Unexpected Http Status code",
fmt.Sprintf("expected (%d) got (%s)", http.StatusNoContent, resp.Status))
return diags
}
diags.Append(IsResponseValid(resp, err, http.StatusNoContent)...)
return diags
}

Expand Down Expand Up @@ -261,20 +237,8 @@ func (r GroupResource) UpdateGroup(data GroupResourceModelInterface) diag.Diagno
req_data = bytes.NewReader(req_body)
}
resp, body, err := r.client.doRequest(http.MethodPut, data.GetURL(), req_data)
diags.Append(IsResponseValid(resp, err, http.StatusOK)...)

if err != nil {
diags.AddError("Body JSON Marshal Error", err.Error())
return diags
}
if resp == nil {
diags.AddError("Http response Error", "no http response from server")
return diags
}
if resp.StatusCode != http.StatusOK {
diags.AddError("Unexpected Http Status code",
fmt.Sprintf("expected (%d) got (%s)", http.StatusOK, resp.Status))
return diags
}
err = data.ParseHttpResponse(body)
if err != nil {
diags.AddError("error while parsing the json response: ", err.Error())
Expand All @@ -301,18 +265,7 @@ func (r GroupResource) ReadGroup(data GroupResourceModelInterface) diag.Diagnost
// Read existing Group
group_url := data.GetURL()
resp, body, err := r.client.doRequest(http.MethodGet, group_url, nil)
if err != nil {
diags.AddError("Get Error", err.Error())
return diags
}
if resp == nil {
diags.AddError("Http response Error", "no http response from server")
return diags
}
if resp.StatusCode != http.StatusOK {
diags.AddError("Unexpected Http Status code",
fmt.Sprintf("expected (%d) got (%s)", http.StatusOK, resp.Status))
}
diags.Append(IsResponseValid(resp, err, http.StatusOK)...)

err = data.ParseHttpResponse(body)
if err != nil {
Expand Down
Loading
Loading