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

Conversation

alinabuzachis
Copy link
Contributor

@alinabuzachis alinabuzachis commented Jan 19, 2024

Add Host resource

@alinabuzachis alinabuzachis changed the title Add Host resource [wip] Add Host resource Jan 19, 2024
@alinabuzachis alinabuzachis changed the title [wip] Add Host resource Add Host resource Jan 19, 2024
@alinabuzachis alinabuzachis force-pushed the host_resource branch 2 times, most recently from ff59533 to 4a2f37a Compare January 19, 2024 16:39
Signed-off-by: Alina Buzachis <[email protected]>
Copy link
Contributor

@hakbailey hakbailey left a comment

Choose a reason for hiding this comment

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

I don't know if it should be part of this PR (@gravesm?), but we will need to add ID as an attribute to the Group resource so someone can create a group and use that group's ID to add it to a host all within a single terraform config file.

},
"instance_id": schema.StringAttribute{
Optional: true,
Computed: true,
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't think this should be computed, only optional. From the Terraform side, if this isn't provided it will just be null. Same for description.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

instance_id is computed because, supposing the user does not set it, so, it will be passed as nil, it will be returned by the api as an empty string. Same for description. This is my experience and understanding.

{
 ....
  },
  "created": "2024-01-18T16:54:45.198378Z",
  "modified": "2024-01-18T16:54:45.198387Z",
  "name": "tf_host",
  "description": "",
  "inventory": 1,
  "enabled": true,
  "instance_id": "",
  "variables": "",
  "has_active_failures": false,
  "has_inventory_sources": false,
  "last_job": null,
  "last_job_host_summary": null,
  "ansible_facts_modified": null
}

Copy link
Contributor

Choose a reason for hiding this comment

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

Yes from the API side it's an empty string but from the Terraform config perspective it can just remain null.

stringplanmodifier.UseStateForUnknown(),
},
},
"variables": schema.StringAttribute{
Copy link
Contributor

Choose a reason for hiding this comment

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

This should have a CustomType: jsontypes.NormalizedType{} like it does in the other resources so it will validate JSON strings.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I've followed what group_resource has defined as types for variables https://github.com/ansible/terraform-provider-aap/blob/main/internal/provider/group_resource.go#L66 I can fix that, but let's try to be consistent then.

Copy link
Contributor

Choose a reason for hiding this comment

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

Oooh I totally missed that in the group resource! Yes we should update it there too!

Computed: true,
Description: "Defaults true.",
},
"group_id": schema.Int64Attribute{
Copy link
Contributor

Choose a reason for hiding this comment

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

I think this should be a list so the host can be associated with multiple groups.

Copy link
Contributor Author

@alinabuzachis alinabuzachis Jan 22, 2024

Choose a reason for hiding this comment

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

The API requires it to be an integer. In any case, as explained in the other comments, I can allow the user to specify different groups

"groups": schema.ListNestedAttribute{
                NestedObject: schema.NestedAttributeObject{
                    Attributes: map[string]schema.Attribute{
                        "id": schema.Int64Attribute{
                            Required: true,
                        },
						"disassociate": schema.BoolAttribute{
                            Optional: true,
                        },
                    },
                },
                Optional: true,
            },
		},

but we will basically pass to the api an id: int and an optional disassociate flag.

Optional: true,
Description: "Set this option to associate an existing group with a host.",
},
"disassociate_group": schema.BoolAttribute{
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't think this is necessary. Once the associated groups are a list, user should be able to disassociate a group from the host just by removing the group from its list of group IDs in the terraform config.

Copy link
Contributor Author

@alinabuzachis alinabuzachis Jan 22, 2024

Choose a reason for hiding this comment

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

A group is not removed from a host unless disassociate parameter is passed. See https://docs.ansible.com/automation-controller/latest/html/controllerapi/api_ref.html#/Hosts/Hosts_hosts_groups_create Also, id must be passed as an integer and a list is not accepted, this is my understanding.

I can implement something like:

 groups = [
    {
      "id" : 2
    },
    {
      "id": 3
      "disassociate": true
    }
  ]
}

It will mainly make a group association for every id.

Copy link
Member

Choose a reason for hiding this comment

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

We need to do the work of figuring out which groups need to be added/removed on our side. We'll need to list the groups a host is a member of, compare that to what's in the tf config, and then add and remove as necessary. The terraform config should look something like:

resource "aap_host" "foo_host" {
  ...
  groups = [group_a.id, group_b.id]
}

resource "aap_group" "group_a" {}
resource "aap_group" "group_b" {}

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

@gravesm
Copy link
Member

gravesm commented Jan 22, 2024

I don't know if it should be part of this PR (@gravesm?), but we will need to add ID as an attribute to the Group resource so someone can create a group and use that group's ID to add it to a host all within a single terraform config file.

Let's add it to this PR.

// and append the URL from the state data to the plan data.
resp.Diagnostics.Append(req.Plan.Get(ctx, &data)...)
resp.Diagnostics.Append(req.State.Get(ctx, &data_with_URL)...)
data.URL = data_with_URL.URL
Copy link
Contributor

Choose a reason for hiding this comment

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

This is not needed. The URL will be retrieved from the state and populated in the plan.

@alinabuzachis
Copy link
Contributor Author

Close in favor of #6

@gravesm
Copy link
Member

gravesm commented Jan 31, 2024

Why open a new PR?

@alinabuzachis
Copy link
Contributor Author

Why open a new PR?

Because I messed up the branch.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants