|
| 1 | +package grafana |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + |
| 6 | + "github.com/hashicorp/terraform-plugin-sdk/v2/diag" |
| 7 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" |
| 8 | +) |
| 9 | + |
| 10 | +func DataSourceStack() *schema.Resource { |
| 11 | + return &schema.Resource{ |
| 12 | + Description: "Data source for Grafana Stack", |
| 13 | + ReadContext: datasourceStackRead, |
| 14 | + Schema: map[string]*schema.Schema{ |
| 15 | + "id": { |
| 16 | + Type: schema.TypeString, |
| 17 | + Computed: true, |
| 18 | + Description: "The stack id assigned to this stack by Grafana.", |
| 19 | + }, |
| 20 | + "slug": { |
| 21 | + Type: schema.TypeString, |
| 22 | + Required: true, |
| 23 | + Description: ` |
| 24 | +Subdomain that the Grafana instance will be available at (i.e. setting slug to “<stack_slug>” will make the instance |
| 25 | +available at “https://<stack_slug>.grafana.net".`, |
| 26 | + }, |
| 27 | + "name": { |
| 28 | + Type: schema.TypeString, |
| 29 | + Computed: true, |
| 30 | + Description: "Name of stack. Conventionally matches the url of the instance (e.g. “<stack_slug>.grafana.net”).", |
| 31 | + }, |
| 32 | + "description": { |
| 33 | + Type: schema.TypeString, |
| 34 | + Computed: true, |
| 35 | + Description: "Description of stack.", |
| 36 | + }, |
| 37 | + "region_slug": { |
| 38 | + Type: schema.TypeString, |
| 39 | + Computed: true, |
| 40 | + Description: "The region this stack is deployed to.", |
| 41 | + }, |
| 42 | + "url": { |
| 43 | + Type: schema.TypeString, |
| 44 | + Computed: true, |
| 45 | + Description: "Custom URL for the Grafana instance. Must have a CNAME setup to point to `.grafana.net` before creating the stack", |
| 46 | + }, |
| 47 | + "org_id": { |
| 48 | + Type: schema.TypeInt, |
| 49 | + Computed: true, |
| 50 | + Description: "Organization id to assign to this stack.", |
| 51 | + }, |
| 52 | + "org_slug": { |
| 53 | + Type: schema.TypeString, |
| 54 | + Computed: true, |
| 55 | + Description: "Organization slug to assign to this stack.", |
| 56 | + }, |
| 57 | + "org_name": { |
| 58 | + Type: schema.TypeString, |
| 59 | + Computed: true, |
| 60 | + Description: "Organization name to assign to this stack.", |
| 61 | + }, |
| 62 | + "status": { |
| 63 | + Type: schema.TypeString, |
| 64 | + Computed: true, |
| 65 | + Description: "Status of the stack.", |
| 66 | + }, |
| 67 | + "prometheus_user_id": { |
| 68 | + Type: schema.TypeInt, |
| 69 | + Computed: true, |
| 70 | + Description: "Promehteus user ID. Used for e.g. remote_write.", |
| 71 | + }, |
| 72 | + "prometheus_url": { |
| 73 | + Type: schema.TypeString, |
| 74 | + Computed: true, |
| 75 | + Description: "Prometheus url for this instance.", |
| 76 | + }, |
| 77 | + "prometheus_name": { |
| 78 | + Type: schema.TypeString, |
| 79 | + Computed: true, |
| 80 | + Description: "Prometheus name for this instance.", |
| 81 | + }, |
| 82 | + "prometheus_remote_endpoint": { |
| 83 | + Type: schema.TypeString, |
| 84 | + Computed: true, |
| 85 | + Description: "Use this URL to query hosted metrics data e.g. Prometheus data source in Grafana", |
| 86 | + }, |
| 87 | + "prometheus_remote_write_endpoint": { |
| 88 | + Type: schema.TypeString, |
| 89 | + Computed: true, |
| 90 | + Description: "Use this URL to send prometheus metrics to Grafana cloud", |
| 91 | + }, |
| 92 | + "prometheus_status": { |
| 93 | + Type: schema.TypeString, |
| 94 | + Computed: true, |
| 95 | + Description: "Prometheus status for this instance.", |
| 96 | + }, |
| 97 | + "alertmanager_user_id": { |
| 98 | + Type: schema.TypeInt, |
| 99 | + Computed: true, |
| 100 | + Description: "User ID of the Alertmanager instance configured for this stack.", |
| 101 | + }, |
| 102 | + "alertmanager_name": { |
| 103 | + Type: schema.TypeString, |
| 104 | + Computed: true, |
| 105 | + Description: "Name of the Alertmanager instance configured for this stack.", |
| 106 | + }, |
| 107 | + "alertmanager_url": { |
| 108 | + Type: schema.TypeString, |
| 109 | + Computed: true, |
| 110 | + Description: "Base URL of the Alertmanager instance configured for this stack.", |
| 111 | + }, |
| 112 | + "alertmanager_status": { |
| 113 | + Type: schema.TypeString, |
| 114 | + Computed: true, |
| 115 | + Description: "Status of the Alertmanager instance configured for this stack.", |
| 116 | + }, |
| 117 | + }, |
| 118 | + } |
| 119 | +} |
| 120 | + |
| 121 | +func datasourceStackRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { |
| 122 | + client := meta.(*client).gcloudapi |
| 123 | + |
| 124 | + var diags diag.Diagnostics |
| 125 | + |
| 126 | + slug := d.Get("slug").(string) |
| 127 | + |
| 128 | + stack, err := client.StackBySlug(slug) |
| 129 | + if err != nil { |
| 130 | + return diag.FromErr(err) |
| 131 | + } |
| 132 | + |
| 133 | + FlattenStack(d, stack) |
| 134 | + |
| 135 | + return diags |
| 136 | +} |
0 commit comments