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

Make agent_pool_id optional in resource azuredevops_agent_queue #906

Merged
merged 3 commits into from
Nov 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
44 changes: 31 additions & 13 deletions azuredevops/internal/service/taskagent/resource_agent_queue.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
)

const (
agentQueueName = "name"
agentPoolID = "agent_pool_id"
projectID = "project_id"
invalidQueueIDErrorMessageFormat = "Queue ID was unexpectedly not a valid integer: %+v"
Expand All @@ -29,10 +30,21 @@ func ResourceAgentQueue() *schema.Resource {
Delete: resourceAgentQueueDelete,
Importer: tfhelper.ImportProjectQualifiedResourceInteger(),
Schema: map[string]*schema.Schema{
agentQueueName: {
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
AtLeastOneOf: []string{agentPoolID, agentQueueName},
ConflictsWith: []string{agentPoolID},
},
agentPoolID: {
Type: schema.TypeInt,
Required: true,
ForceNew: true,
Type: schema.TypeInt,
Optional: true,
Computed: true,
ForceNew: true,
AtLeastOneOf: []string{agentPoolID, agentQueueName},
ConflictsWith: []string{agentQueueName},
},
projectID: {
Type: schema.TypeString,
Expand All @@ -52,14 +64,18 @@ func resourceAgentQueueCreate(d *schema.ResourceData, m interface{}) error {
return fmt.Errorf("Error expanding the agent queue resource from state: %+v", err)
}

referencedPool, err := clients.TaskAgentClient.GetAgentPool(clients.Ctx, taskagent.GetAgentPoolArgs{
PoolId: queue.Pool.Id,
})
if err != nil {
return fmt.Errorf("Error looking up referenced agent pool: %+v", err)
if queue.Pool != nil {
referencedPool, err := clients.TaskAgentClient.GetAgentPool(clients.Ctx, taskagent.GetAgentPoolArgs{
PoolId: queue.Pool.Id,
})
if err != nil {
return fmt.Errorf("Error looking up referenced agent pool: %+v", err)
}
queue.Name = referencedPool.Name
} else {
queue.Name = converter.String(d.Get(agentQueueName).(string))
}

queue.Name = referencedPool.Name
createdQueue, err := clients.TaskAgentClient.AddAgentQueue(clients.Ctx, taskagent.AddAgentQueueArgs{
Queue: queue,
Project: &projectID,
Expand All @@ -75,10 +91,12 @@ func resourceAgentQueueCreate(d *schema.ResourceData, m interface{}) error {
}

func expandAgentQueue(d *schema.ResourceData) (*taskagent.TaskAgentQueue, string, error) {
queue := &taskagent.TaskAgentQueue{
Pool: &taskagent.TaskAgentPoolReference{
Id: converter.Int(d.Get(agentPoolID).(int)),
},
queue := &taskagent.TaskAgentQueue{}

if v, exist := d.GetOk(agentPoolID); exist {
queue.Pool = &taskagent.TaskAgentPoolReference{
Id: converter.Int(v.(int)),
}
}

if d.Id() != "" {
Expand Down
22 changes: 21 additions & 1 deletion website/docs/r/agent_queue.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ the `azuredevops_resource_authorization` resource can be used to grant authoriza

## Example Usage

### Creating a Queue from an organization-level pool

```hcl
resource "azuredevops_project" "example" {
name = "Example Project"
Expand All @@ -38,12 +40,30 @@ resource "azuredevops_resource_authorization" "example" {
}
```

### Creating a Queue at the project level (Organization-level permissions not required)

```hcl
data "azuredevops_project" "example" {
name = "Example Project"
}

resource "azuredevops_agent_queue" "example" {
name = "example-queue"
project_id = data.azuredevops_project.example.id
}
```

## Argument Reference

The following arguments are supported:

- `name` - (Optional) The name of the agent queue. Defaults to the ID of the agent pool. Conflicts with `agent_pool_id`.
- `project_id` - (Required) The ID of the project in which to create the resource.
- `agent_pool_id` - (Required) The ID of the organization agent pool.
- `agent_pool_id` - (Optional) The ID of the organization agent pool. Conflicts with `name`.

~> **NOTE:**
One of `name` or `agent_pool_id` must be specified, but not both.
When `agent_pool_id` is specified, the agent queue name will be derived from the agent pool name.

## Attributes Reference

Expand Down