|
| 1 | +package gcore |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "log" |
| 6 | + |
| 7 | + "github.com/G-Core/gcorelabscloud-go/gcore/loadbalancer/v1/loadbalancers" |
| 8 | + "github.com/hashicorp/terraform-plugin-sdk/v2/diag" |
| 9 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" |
| 10 | +) |
| 11 | + |
| 12 | +func dataSourceLoadBalancerV2() *schema.Resource { |
| 13 | + return &schema.Resource{ |
| 14 | + ReadContext: dataSourceLoadBalancerV2Read, |
| 15 | + Schema: map[string]*schema.Schema{ |
| 16 | + "project_id": &schema.Schema{ |
| 17 | + Type: schema.TypeInt, |
| 18 | + Optional: true, |
| 19 | + ExactlyOneOf: []string{ |
| 20 | + "project_id", |
| 21 | + "project_name", |
| 22 | + }, |
| 23 | + }, |
| 24 | + "region_id": &schema.Schema{ |
| 25 | + Type: schema.TypeInt, |
| 26 | + Optional: true, |
| 27 | + ExactlyOneOf: []string{ |
| 28 | + "region_id", |
| 29 | + "region_name", |
| 30 | + }, |
| 31 | + }, |
| 32 | + "project_name": &schema.Schema{ |
| 33 | + Type: schema.TypeString, |
| 34 | + Optional: true, |
| 35 | + ExactlyOneOf: []string{ |
| 36 | + "project_id", |
| 37 | + "project_name", |
| 38 | + }, |
| 39 | + }, |
| 40 | + "region_name": &schema.Schema{ |
| 41 | + Type: schema.TypeString, |
| 42 | + Optional: true, |
| 43 | + ExactlyOneOf: []string{ |
| 44 | + "region_id", |
| 45 | + "region_name", |
| 46 | + }, |
| 47 | + }, |
| 48 | + "name": &schema.Schema{ |
| 49 | + Type: schema.TypeString, |
| 50 | + Required: true, |
| 51 | + }, |
| 52 | + "vip_address": &schema.Schema{ |
| 53 | + Type: schema.TypeString, |
| 54 | + Computed: true, |
| 55 | + }, |
| 56 | + "vip_port_id": &schema.Schema{ |
| 57 | + Type: schema.TypeString, |
| 58 | + Computed: true, |
| 59 | + }, |
| 60 | + }, |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +func dataSourceLoadBalancerV2Read(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { |
| 65 | + log.Println("[DEBUG] Start LoadBalancer reading") |
| 66 | + var diags diag.Diagnostics |
| 67 | + config := m.(*Config) |
| 68 | + provider := config.Provider |
| 69 | + |
| 70 | + client, err := CreateClient(provider, d, LoadBalancersPoint, versionPointV1) |
| 71 | + if err != nil { |
| 72 | + return diag.FromErr(err) |
| 73 | + } |
| 74 | + |
| 75 | + name := d.Get("name").(string) |
| 76 | + lbs, err := loadbalancers.ListAll(client) |
| 77 | + if err != nil { |
| 78 | + return diag.FromErr(err) |
| 79 | + } |
| 80 | + |
| 81 | + var found bool |
| 82 | + var lb loadbalancers.LoadBalancer |
| 83 | + for _, l := range lbs { |
| 84 | + if l.Name == name { |
| 85 | + lb = l |
| 86 | + found = true |
| 87 | + break |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + if !found { |
| 92 | + return diag.Errorf("load balancer with name %s not found", name) |
| 93 | + } |
| 94 | + |
| 95 | + d.SetId(lb.ID) |
| 96 | + d.Set("project_id", lb.ProjectID) |
| 97 | + d.Set("region_id", lb.RegionID) |
| 98 | + d.Set("name", lb.Name) |
| 99 | + d.Set("vip_address", lb.VipAddress.String()) |
| 100 | + d.Set("vip_port_id", lb.VipPortID) |
| 101 | + |
| 102 | + log.Println("[DEBUG] Finish LoadBalancer reading") |
| 103 | + return diags |
| 104 | +} |
0 commit comments