|
| 1 | +// Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved. |
| 2 | + |
| 3 | +package provider |
| 4 | + |
| 5 | +import ( |
| 6 | + "context" |
| 7 | + |
| 8 | + "github.com/hashicorp/terraform/helper/schema" |
| 9 | + oci_load_balancer "github.com/oracle/oci-go-sdk/loadbalancer" |
| 10 | + |
| 11 | + "github.com/oracle/terraform-provider-oci/crud" |
| 12 | +) |
| 13 | + |
| 14 | +func BackendHealthDataSource() *schema.Resource { |
| 15 | + return &schema.Resource{ |
| 16 | + Read: readSingularBackendHealth, |
| 17 | + Schema: map[string]*schema.Schema{ |
| 18 | + "backend_name": { |
| 19 | + Type: schema.TypeString, |
| 20 | + Required: true, |
| 21 | + }, |
| 22 | + "backend_set_name": { |
| 23 | + Type: schema.TypeString, |
| 24 | + Required: true, |
| 25 | + }, |
| 26 | + "load_balancer_id": { |
| 27 | + Type: schema.TypeString, |
| 28 | + Required: true, |
| 29 | + }, |
| 30 | + // Computed |
| 31 | + "health_check_results": { |
| 32 | + Type: schema.TypeList, |
| 33 | + Computed: true, |
| 34 | + Elem: &schema.Resource{ |
| 35 | + Schema: map[string]*schema.Schema{ |
| 36 | + // Required |
| 37 | + |
| 38 | + // Optional |
| 39 | + |
| 40 | + // Computed |
| 41 | + "health_check_status": { |
| 42 | + Type: schema.TypeString, |
| 43 | + Computed: true, |
| 44 | + }, |
| 45 | + "source_ip_address": { |
| 46 | + Type: schema.TypeString, |
| 47 | + Computed: true, |
| 48 | + }, |
| 49 | + "subnet_id": { |
| 50 | + Type: schema.TypeString, |
| 51 | + Computed: true, |
| 52 | + }, |
| 53 | + "timestamp": { |
| 54 | + Type: schema.TypeString, |
| 55 | + Computed: true, |
| 56 | + }, |
| 57 | + }, |
| 58 | + }, |
| 59 | + }, |
| 60 | + "status": { |
| 61 | + Type: schema.TypeString, |
| 62 | + Computed: true, |
| 63 | + }, |
| 64 | + }, |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +func readSingularBackendHealth(d *schema.ResourceData, m interface{}) error { |
| 69 | + sync := &BackendHealthDataSourceCrud{} |
| 70 | + sync.D = d |
| 71 | + sync.Client = m.(*OracleClients).loadBalancerClient |
| 72 | + |
| 73 | + return crud.ReadResource(sync) |
| 74 | +} |
| 75 | + |
| 76 | +type BackendHealthDataSourceCrud struct { |
| 77 | + D *schema.ResourceData |
| 78 | + Client *oci_load_balancer.LoadBalancerClient |
| 79 | + Res *oci_load_balancer.GetBackendHealthResponse |
| 80 | +} |
| 81 | + |
| 82 | +func (s *BackendHealthDataSourceCrud) VoidState() { |
| 83 | + s.D.SetId("") |
| 84 | +} |
| 85 | + |
| 86 | +func (s *BackendHealthDataSourceCrud) Get() error { |
| 87 | + request := oci_load_balancer.GetBackendHealthRequest{} |
| 88 | + |
| 89 | + if backendName, ok := s.D.GetOkExists("backend_name"); ok { |
| 90 | + tmp := backendName.(string) |
| 91 | + request.BackendName = &tmp |
| 92 | + } |
| 93 | + |
| 94 | + if backendSetName, ok := s.D.GetOkExists("backend_set_name"); ok { |
| 95 | + tmp := backendSetName.(string) |
| 96 | + request.BackendSetName = &tmp |
| 97 | + } |
| 98 | + |
| 99 | + if loadBalancerId, ok := s.D.GetOkExists("load_balancer_id"); ok { |
| 100 | + tmp := loadBalancerId.(string) |
| 101 | + request.LoadBalancerId = &tmp |
| 102 | + } |
| 103 | + |
| 104 | + request.RequestMetadata.RetryPolicy = getRetryPolicy(false, "load_balancer") |
| 105 | + |
| 106 | + response, err := s.Client.GetBackendHealth(context.Background(), request) |
| 107 | + if err != nil { |
| 108 | + return err |
| 109 | + } |
| 110 | + |
| 111 | + s.Res = &response |
| 112 | + return nil |
| 113 | +} |
| 114 | + |
| 115 | +func (s *BackendHealthDataSourceCrud) SetData() { |
| 116 | + if s.Res == nil { |
| 117 | + return |
| 118 | + } |
| 119 | + |
| 120 | + s.D.SetId(crud.GenerateDataSourceID()) |
| 121 | + |
| 122 | + healthCheckResults := []interface{}{} |
| 123 | + for _, item := range s.Res.HealthCheckResults { |
| 124 | + healthCheckResults = append(healthCheckResults, HealthCheckResultToMap(item)) |
| 125 | + } |
| 126 | + s.D.Set("health_check_results", healthCheckResults) |
| 127 | + |
| 128 | + s.D.Set("status", s.Res.Status) |
| 129 | + |
| 130 | + return |
| 131 | +} |
| 132 | + |
| 133 | +func HealthCheckResultToMap(obj oci_load_balancer.HealthCheckResult) map[string]interface{} { |
| 134 | + result := map[string]interface{}{} |
| 135 | + |
| 136 | + result["health_check_status"] = string(obj.HealthCheckStatus) |
| 137 | + |
| 138 | + if obj.SourceIpAddress != nil { |
| 139 | + result["source_ip_address"] = string(*obj.SourceIpAddress) |
| 140 | + } |
| 141 | + |
| 142 | + if obj.SubnetId != nil { |
| 143 | + result["subnet_id"] = string(*obj.SubnetId) |
| 144 | + } |
| 145 | + |
| 146 | + if obj.Timestamp != nil { |
| 147 | + result["timestamp"] = obj.Timestamp.String() |
| 148 | + } |
| 149 | + |
| 150 | + return result |
| 151 | +} |
0 commit comments