Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Add support for a load balancer data source #529

Open
1 task done
bmonselet opened this issue Jun 20, 2024 · 2 comments
Open
1 task done

Add support for a load balancer data source #529

bmonselet opened this issue Jun 20, 2024 · 2 comments
Assignees
Labels
enhancement Enhancement

Comments

@bmonselet
Copy link

bmonselet commented Jun 20, 2024

Code of Conduct

This project has a Code of Conduct that all participants are expected to understand and follow:

Description

We can create Load Balancer as a resource but we don't the data source of it.

It will be great if we can get all the info from a LB after is creation.

References

Community Note

  • Please vote on this issue by adding a 👍 reaction to the original issue to help the community and maintainers prioritize this request
  • Please do not leave "+1" or other comments that do not add relevant new information or questions, they generate extra noise for issue followers and do not help prioritize the request
  • If you are interested in working on this issue or have submitted a pull request, please leave a comment
@bmonselet bmonselet added enhancement Enhancement needs-triage Needs Triage labels Jun 20, 2024
@tenthirtyam tenthirtyam removed the needs-triage Needs Triage label Jun 20, 2024
@tenthirtyam tenthirtyam changed the title Data source for Load Balancer Add support for a load balancer data source Jun 20, 2024
Copy link

github-actions bot commented Dec 3, 2024

Marking this issue as stale due to inactivity. This helps us focus on the active issues. If this issue receives no comments in the next 30 days it will automatically be closed.

If this issue was automatically closed and you feel this issue should be reopened, we encourage creating a new issue linking back to this one for added context.
Thank you!

@github-actions github-actions bot added the stale Stale label Dec 3, 2024
@github-actions github-actions bot removed the stale Stale label Feb 11, 2025
@tenthirtyam tenthirtyam self-assigned this Mar 12, 2025
@tenthirtyam
Copy link
Collaborator

Self-assigning.

Possible solution to be added as seen below for a new database source.

func dataSourceLoadBalancer() *schema.Resource {
	return &schema.Resource{
		Read: dataSourceLoadBalancerRead,

		Schema: map[string]*schema.Schema{
			"id": {
				Type:          schema.TypeString,
				Optional:      true,
				Computed:      true,
				Description:   "The ID of the Load Balancer instance. Only one of 'filter', 'id', 'name', or 'region_id' must be specified.",
				ConflictsWith: []string{"filter", "name", "region_id"},
			},
			"name": {
				Type:          schema.TypeString,
				Optional:      true,
				Computed:      true,
				Description:   "A human-friendly name used as an identifier in APIs that support this option. Only one of 'filter', 'id', 'name', or 'region_id' must be specified.",
				ConflictsWith: []string{"filter", "id", "region_id"},
			},
			"region_id": {
				Type:          schema.TypeString,
				Optional:      true,
				Computed:      true,
				Description:   "The ID of the region for which this Load Balancer is defined. Only one of 'filter', 'id', 'name', or 'region_id' must be specified.",
				ConflictsWith: []string{"filter", "id", "name"},
			},
			"filter": {
				Type:          schema.TypeString,
				Optional:      true,
				Description:   "Filter query string that is supported by vRA multi-cloud IaaS API. Example: regionId eq '<regionId>' and cloudAccountId eq '<cloudAccountId>'. Only one of 'filter', 'id', 'name', or 'region_id' must be specified.",
				ConflictsWith: []string{"id", "name", "region_id"},
			},
			"address": {
				Type:        schema.TypeString,
				Computed:    true,
				Description: "The IP address or DNS name of the Load Balancer.",
			},
			"created_at": {
				Type:        schema.TypeString,
				Computed:    true,
				Description: "The date and time when the Load Balancer was created.",
			},
			"custom_properties": {
				Type:        schema.TypeMap,
				Computed:    true,
				Description: "A map of custom properties associated with the Load Balancer.",
			},
			"description": {
				Type:        schema.TypeString,
				Computed:    true,
				Description: "A human-friendly description of the Load Balancer.",
			},
			"deployment_id": {
				Type:        schema.TypeString,
				Computed:    true,
				Description: "The ID of the deployment associated with the Load Balancer.",
			},
			"external_id": {
				Type:        schema.TypeString,
				Computed:    true,
				Description: "The external ID of the Load Balancer as assigned by the cloud provider.",
			},
			"external_region_id": {
				Type:        schema.TypeString,
				Computed:    true,
				Description: "The ID of the external region where the Load Balancer is located.",
			},
			"external_zone_id": {
				Type:        schema.TypeString,
				Computed:    true,
				Description: "The ID of the external zone where the Load Balancer is located.",
			},
			"organization_id": {
				Type:        schema.TypeString,
				Computed:    true,
				Description: "The ID of the organization that owns the Load Balancer.",
			},
			"owner": {
				Type:        schema.TypeString,
				Computed:    true,
				Description: "The email of the user who owns the Load Balancer.",
			},
			"project_id": {
				Type:        schema.TypeString,
				Computed:    true,
				Description: "The ID of the project associated with the Load Balancer.",
			},
			"updated_at": {
				Type:        schema.TypeString,
				Computed:    true,
				Description: "The date and time when the Load Balancer was last updated.",
			},
			"tags": {
				Type:        schema.TypeList,
				Computed:    true,
				Description: "A list of tags associated with the Load Balancer.",
				Elem: &schema.Schema{
					Type: schema.TypeString,
				},
			},
			"routes": {
				Type:        schema.TypeList,
				Computed:    true,
				Description: "A list of routes configured for the Load Balancer.",
				Elem: &schema.Schema{
					Type: schema.TypeMap,
				},
			},
			"links": {
				Type:        schema.TypeList,
				Computed:    true,
				Description: "A list of links associated with the Load Balancer.",
				Elem: &schema.Schema{
					Type: schema.TypeMap,
				},
			},
		},
	}
}

func dataSourceLoadBalancerRead(d *schema.ResourceData, m interface{}) error {
	apiClient := m.(*Client).apiClient
	var loadBalancer *models.LoadBalancer
	var filter string

	id := d.Get("id").(string)
	name := d.Get("name").(string)
	configFilter := d.Get("filter").(string)
	regionID := d.Get("region_id").(string)

	if id == "" && name == "" && configFilter == "" && regionID == "" {
		return fmt.Errorf("one of (id, name, region_id, filter) is required")
	}

	setFields := func(lb *models.LoadBalancer) error {
		d.SetId(*lb.ID)
		d.Set("address", lb.Address)
		d.Set("created_at", lb.CreatedAt)
		d.Set("custom_properties", lb.CustomProperties)
		d.Set("description", lb.Description)
		d.Set("deployment_id", lb.DeploymentID)
		d.Set("external_id", lb.ExternalID)
		d.Set("external_region_id", lb.ExternalRegionID)
		d.Set("external_zone_id", lb.ExternalZoneID)
		d.Set("name", lb.Name)
		d.Set("organization_id", lb.OrgID)
		d.Set("owner", lb.Owner)
		d.Set("project_id", lb.ProjectID)
		d.Set("updated_at", lb.UpdatedAt)

		if err := d.Set("tags", flattenTags(lb.Tags)); err != nil {
			return fmt.Errorf("error setting load balancer tags - error: %v", err)
		}
		if err := d.Set("routes", flattenRoutes(lb.Routes)); err != nil {
			return fmt.Errorf("error setting load balancer routes - error: %v", err)
		}
		if err := d.Set("links", flattenLinks(lb.Links)); err != nil {
			return fmt.Errorf("error setting load balancer links - error: %v", err)
		}

		return nil
	}

	if id != "" {
		getResp, err := apiClient.LoadBalancer.GetLoadBalancer(load_balancer.NewGetLoadBalancerParams().WithID(id))
		if err != nil {
			return err
		}
		loadBalancer = getResp.Payload
		return setFields(loadBalancer)
	} else if regionID != "" {
		filter = fmt.Sprintf("regionId eq '%v'", regionID)
	} else if name != "" {
		filter = fmt.Sprintf("name eq '%v'", name)
	} else if configFilter != "" {
		filter = configFilter
	}

	getResp, err := apiClient.LoadBalancer.GetLoadBalancers(load_balancer.NewGetLoadBalancersParams().WithDollarFilter(withString(filter)))
	if err != nil {
		return err
	}

	loadBalancers := *getResp.Payload
	if len(loadBalancers.Content) > 1 {
		return fmt.Errorf("vra_load_balancer must filter to a single load balancer")
	}
	if len(loadBalancers.Content) == 0 {
		return fmt.Errorf("vra_load_balancer filter did not match any load balancer")
	}

	loadBalancer = loadBalancers.Content[0]
	return setFields(loadBalancer)
}

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
enhancement Enhancement
Projects
None yet
Development

No branches or pull requests

2 participants