|
| 1 | +package sumologic |
| 2 | + |
| 3 | +import ( |
| 4 | + "crypto/sha256" |
| 5 | + "encoding/hex" |
| 6 | + "fmt" |
| 7 | + "sort" |
| 8 | + "strings" |
| 9 | + |
| 10 | + "github.com/hashicorp/terraform-plugin-sdk/helper/schema" |
| 11 | +) |
| 12 | + |
| 13 | +func dataSourceSumologicPartitions() *schema.Resource { |
| 14 | + return &schema.Resource{ |
| 15 | + Read: dataSourceSumologicPartitionsRead, |
| 16 | + |
| 17 | + Schema: map[string]*schema.Schema{ |
| 18 | + "partitions": { |
| 19 | + Type: schema.TypeList, |
| 20 | + Computed: true, |
| 21 | + Elem: &schema.Resource{ |
| 22 | + Schema: dataSourcePartitionSchema(), |
| 23 | + Read: dataSourceSumologicPartitionRead, |
| 24 | + }, |
| 25 | + }, |
| 26 | + }, |
| 27 | + } |
| 28 | +} |
| 29 | + |
| 30 | +func dataSourceSumologicPartitionsRead(d *schema.ResourceData, meta interface{}) error { |
| 31 | + c := meta.(*Client) |
| 32 | + |
| 33 | + spartitions, err := c.ListPartitions() |
| 34 | + if err != nil { |
| 35 | + return fmt.Errorf("error retrieving partitions: %v", err) |
| 36 | + } |
| 37 | + |
| 38 | + partitions := make([]map[string]interface{}, 0, len(spartitions)) |
| 39 | + |
| 40 | + for _, spartition := range spartitions { |
| 41 | + partition := map[string]interface{}{ |
| 42 | + "id": spartition.ID, |
| 43 | + "name": spartition.Name, |
| 44 | + "routing_expression": spartition.RoutingExpression, |
| 45 | + "analytics_tier": spartition.AnalyticsTier, |
| 46 | + "retention_period": spartition.RetentionPeriod, |
| 47 | + "is_compliant": spartition.IsCompliant, |
| 48 | + "total_bytes": spartition.TotalBytes, |
| 49 | + "data_forwarding_id": spartition.DataForwardingId, |
| 50 | + "is_active": spartition.IsActive, |
| 51 | + "index_type": spartition.IndexType, |
| 52 | + "is_included_in_default_search": spartition.IsIncludedInDefaultSearch, |
| 53 | + } |
| 54 | + |
| 55 | + partitions = append(partitions, partition) |
| 56 | + } |
| 57 | + |
| 58 | + d.Set("partitions", partitions) |
| 59 | + d.SetId(generatePartitionsId(spartitions)) |
| 60 | + |
| 61 | + return nil |
| 62 | +} |
| 63 | + |
| 64 | +func generatePartitionsId(partitions []Partition) string { |
| 65 | + // Collect a sorted list of partition IDs |
| 66 | + ids := []string{"partition_ids"} |
| 67 | + for _, partition := range partitions { |
| 68 | + ids = append(ids, partition.ID) |
| 69 | + } |
| 70 | + sort.Strings(ids) |
| 71 | + |
| 72 | + // Join all IDs and create a hash |
| 73 | + idString := strings.Join(ids, "|") |
| 74 | + hash := sha256.Sum256([]byte(idString)) |
| 75 | + return hex.EncodeToString(hash[:]) |
| 76 | +} |
0 commit comments