Skip to content

Commit

Permalink
Enable oncall client setup using service account auth
Browse files Browse the repository at this point in the history
  • Loading branch information
matiasb committed Dec 16, 2024
1 parent 101c855 commit 6391402
Show file tree
Hide file tree
Showing 7 changed files with 131 additions and 3 deletions.
40 changes: 40 additions & 0 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,46 @@ resource "grafana_oncall_escalation" "example_notify_step" {
}
```

Alternatively, you can also configure the provider block by setting `url`
to your Grafana URL and `auth` to a service account token:

```terraform
// Step 1: Configure provider block.
provider "grafana" {
alias = "oncall"
url = "http://grafana.example.com/"
auth = var.grafana_auth
}
data "grafana_oncall_user" "alex" {
username = "alex"
}
// Step 2: Interact with Grafana OnCall
resource "grafana_oncall_integration" "test-acc-integration" {
provider = grafana.oncall
name = "my integration"
type = "grafana"
default_route {
escalation_chain_id = grafana_oncall_escalation_chain.default.id
}
}
resource "grafana_oncall_escalation_chain" "default" {
provider = grafana.oncall
name = "default"
}
resource "grafana_oncall_escalation" "example_notify_step" {
escalation_chain_id = grafana_oncall_escalation_chain.default.id
type = "notify_persons"
persons_to_notify = [
data.grafana_oncall_user.alex.id
]
position = 0
}
```

<!-- schema generated by tfplugindocs -->
## Schema

Expand Down
34 changes: 34 additions & 0 deletions examples/provider/provider-oncall-sa.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// Step 1: Configure provider block.
provider "grafana" {
alias = "oncall"
url = "http://grafana.example.com/"
auth = var.grafana_auth
}

data "grafana_oncall_user" "alex" {
username = "alex"
}

// Step 2: Interact with Grafana OnCall
resource "grafana_oncall_integration" "test-acc-integration" {
provider = grafana.oncall
name = "my integration"
type = "grafana"
default_route {
escalation_chain_id = grafana_oncall_escalation_chain.default.id
}
}

resource "grafana_oncall_escalation_chain" "default" {
provider = grafana.oncall
name = "default"
}

resource "grafana_oncall_escalation" "example_notify_step" {
escalation_chain_id = grafana_oncall_escalation_chain.default.id
type = "notify_persons"
persons_to_notify = [
data.grafana_oncall_user.alex.id
]
position = 0
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ require (
github.com/fatih/color v1.18.0
github.com/go-openapi/runtime v0.28.0
github.com/go-openapi/strfmt v0.23.0
github.com/grafana/amixr-api-go-client v0.0.18 // main branch
github.com/grafana/amixr-api-go-client v0.0.19 // main branch
github.com/grafana/grafana-com-public-clients/go/gcom v0.0.0-20240807172819-ac10800522a3
github.com/grafana/grafana-openapi-client-go v0.0.0-20241113095943-9cb2bbfeb8a3
github.com/grafana/machine-learning-go-client v0.8.2
Expand Down
6 changes: 6 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,12 @@ github.com/gorilla/mux v1.8.1 h1:TuBL49tXwgrFYWhqrNgrUNEY92u81SPhu7sTdzQEiWY=
github.com/gorilla/mux v1.8.1/go.mod h1:AKf9I4AEqPTmMytcMc0KkNouC66V3BtZ4qD5fmWSiMQ=
github.com/grafana/amixr-api-go-client v0.0.18 h1:gih3Uug/i9RIz4ihoqWpV3ntb7OmaR0oFfE+zVbwHU0=
github.com/grafana/amixr-api-go-client v0.0.18/go.mod h1:u53FF0WSBMx6XvZK58fply91KBl6X+OtIu0aJC07amY=
github.com/grafana/amixr-api-go-client v0.0.19-0.20241213140536-c007700cedc3 h1:WaP7ahFbKU9sfkMbQLN7+iWKDFVU/ZP11WxQPwLD9b8=
github.com/grafana/amixr-api-go-client v0.0.19-0.20241213140536-c007700cedc3/go.mod h1:u53FF0WSBMx6XvZK58fply91KBl6X+OtIu0aJC07amY=
github.com/grafana/amixr-api-go-client v0.0.19-0.20241216131433-c4dc18332170 h1:Ep4Qc8nhlwM5jfoIuAoOXstMKjteCps9e/4llUSG0/I=
github.com/grafana/amixr-api-go-client v0.0.19-0.20241216131433-c4dc18332170/go.mod h1:u53FF0WSBMx6XvZK58fply91KBl6X+OtIu0aJC07amY=
github.com/grafana/amixr-api-go-client v0.0.19 h1:09FxpE9CXR7YpjDvq/ionA6UlmV4G1r48HFR4noG9Dw=
github.com/grafana/amixr-api-go-client v0.0.19/go.mod h1:u53FF0WSBMx6XvZK58fply91KBl6X+OtIu0aJC07amY=
github.com/grafana/grafana-com-public-clients/go/gcom v0.0.0-20240807172819-ac10800522a3 h1:CVLTffnWgBGvVaXfUUcSgFrZbiMzvj0/Hpi909zdeG0=
github.com/grafana/grafana-com-public-clients/go/gcom v0.0.0-20240807172819-ac10800522a3/go.mod h1:u9d0BESoKlztYm93CpoRleQjMbYBcZ+JOLHHP2nN6Wg=
github.com/grafana/grafana-openapi-client-go v0.0.0-20241113095943-9cb2bbfeb8a3 h1:poKxGlUaEYVp2DMofC/I2GHw/vvtHAZ20c48I8rFB6M=
Expand Down
9 changes: 7 additions & 2 deletions pkg/provider/configure_clients.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func CreateClients(providerConfig ProviderConfig) (*common.Client, error) {
if !providerConfig.SMAccessToken.IsNull() {
c.SMAPI = SMAPI.NewClient(providerConfig.SMURL.ValueString(), providerConfig.SMAccessToken.ValueString(), getRetryClient(providerConfig))
}
if !providerConfig.OncallAccessToken.IsNull() {
if !providerConfig.OncallURL.IsNull() && (!providerConfig.OncallAccessToken.IsNull() || (!providerConfig.Auth.IsNull() && !providerConfig.URL.IsNull())) {
var onCallClient *onCallAPI.Client
onCallClient, err = createOnCallClient(providerConfig)
if err != nil {
Expand Down Expand Up @@ -182,7 +182,12 @@ func createCloudClient(client *common.Client, providerConfig ProviderConfig) err
}

func createOnCallClient(providerConfig ProviderConfig) (*onCallAPI.Client, error) {
return onCallAPI.New(providerConfig.OncallURL.ValueString(), providerConfig.OncallAccessToken.ValueString())
authToken := providerConfig.OncallAccessToken.ValueString()
if authToken == "" {
// prefer OncallAccessToken if it was set, otherwise use Grafana auth (service account) token
authToken = providerConfig.Auth.ValueString()
}
return onCallAPI.NewWithGrafanaURL(providerConfig.OncallURL.ValueString(), authToken, providerConfig.URL.ValueString())
}

func createCloudProviderClient(client *common.Client, providerConfig ProviderConfig) error {
Expand Down
38 changes: 38 additions & 0 deletions pkg/provider/configure_clients_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ func TestCreateClients(t *testing.T) {
assert.NotNil(t, c.GrafanaAPI)
assert.NotNil(t, c.MLAPI)
assert.NotNil(t, c.SLOClient)
assert.Nil(t, c.OnCallClient)
},
},
{
Expand All @@ -133,6 +134,43 @@ func TestCreateClients(t *testing.T) {
assert.NotNil(t, c.GrafanaAPI)
},
},
{
name: "Stack URL and auth to be set, empty strings; OnCall URL set (it has a default)",
config: ProviderConfig{
URL: types.StringValue(""),
Auth: types.StringValue(""),
OncallURL: types.StringValue("http://oncall.url"),
},
expected: func(c *common.Client, err error) {
assert.Nil(t, err)
assert.NotNil(t, c.GrafanaAPI)
},
},
{
name: "OnCall client using original config (not setting Grafana URL)",
config: ProviderConfig{
OncallAccessToken: types.StringValue("oncall-token"),
OncallURL: types.StringValue("http://oncall.url"),
},
expected: func(c *common.Client, err error) {
assert.Nil(t, err)
assert.NotNil(t, c.OnCallClient)
assert.Nil(t, c.OnCallClient.GrafanaURL())
},
},
{
name: "OnCall client setting Grafana URL (using Grafana URL and auth)",
config: ProviderConfig{
URL: types.StringValue("http://localhost:3000"),
Auth: types.StringValue("service-account-token"),
OncallURL: types.StringValue("http://oncall.url"),
},
expected: func(c *common.Client, err error) {
assert.Nil(t, err)
assert.NotNil(t, c.OnCallClient)
assert.Equal(t, "http://localhost:3000", c.OnCallClient.GrafanaURL().String())
},
},
}

for _, tc := range testCases {
Expand Down
5 changes: 5 additions & 0 deletions templates/index.md.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ The changelog for this provider can be found here: <https://github.com/grafana/t

{{ tffile "examples/provider/provider-oncall.tf" }}

Alternatively, you can also configure the provider block by setting `url`
to your Grafana URL and `auth` to a service account token:

{{ tffile "examples/provider/provider-oncall-sa.tf" }}

{{ .SchemaMarkdown | trimspace }}

### Managing Cloud Provider
Expand Down

0 comments on commit 6391402

Please sign in to comment.