Skip to content

Commit

Permalink
Merge branch 'main' into linoman/saml_azure_integration
Browse files Browse the repository at this point in the history
  • Loading branch information
linoman authored Jul 10, 2024
2 parents 7530d35 + f14dcad commit 9e32de6
Show file tree
Hide file tree
Showing 30 changed files with 631 additions and 219 deletions.
40 changes: 35 additions & 5 deletions cmd/generate/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/grafana/terraform-provider-grafana/v3/pkg/generate"

"github.com/fatih/color"
goVersion "github.com/hashicorp/go-version"
"github.com/urfave/cli/v2"
)

Expand Down Expand Up @@ -68,6 +69,24 @@ This supports a glob format. Examples:
EnvVars: []string{"TFGEN_INCLUDE_RESOURCES"},
Required: false,
},
&cli.BoolFlag{
Name: "output-credentials",
Usage: "Output credentials in the generated resources",
EnvVars: []string{"TFGEN_OUTPUT_CREDENTIALS"},
Value: false,
},
&cli.StringFlag{
Name: "terraform-install-dir",
Usage: `Directory to install Terraform to. If not set, a temporary directory will be created.`,
EnvVars: []string{"TFGEN_TERRAFORM_INSTALL_DIR"},
Required: false,
},
&cli.StringFlag{
Name: "terraform-install-version",
Usage: `Version of Terraform to install. If not set, the latest version _tested in this tool_ will be installed.`,
EnvVars: []string{"TFGEN_TERRAFORM_INSTALL_VERSION"},
Required: false,
},

// Grafana OSS flags
&cli.StringFlag{
Expand Down Expand Up @@ -157,10 +176,11 @@ This supports a glob format. Examples:

func parseFlags(ctx *cli.Context) (*generate.Config, error) {
config := &generate.Config{
OutputDir: ctx.String("output-dir"),
Clobber: ctx.Bool("clobber"),
Format: generate.OutputFormat(ctx.String("output-format")),
ProviderVersion: ctx.String("terraform-provider-version"),
OutputDir: ctx.String("output-dir"),
Clobber: ctx.Bool("clobber"),
Format: generate.OutputFormat(ctx.String("output-format")),
ProviderVersion: ctx.String("terraform-provider-version"),
OutputCredentials: ctx.Bool("output-credentials"),
Grafana: &generate.GrafanaConfig{
URL: ctx.String("grafana-url"),
Auth: ctx.String("grafana-auth"),
Expand All @@ -177,14 +197,24 @@ func parseFlags(ctx *cli.Context) (*generate.Config, error) {
StackServiceAccountName: ctx.String("cloud-stack-service-account-name"),
},
IncludeResources: ctx.StringSlice("include-resources"),
TerraformInstallConfig: generate.TerraformInstallConfig{
InstallDir: ctx.String("terraform-install-dir"),
},
}
var err error
if tfVersion := ctx.String("terraform-install-version"); tfVersion != "" {
config.TerraformInstallConfig.Version, err = goVersion.NewVersion(ctx.String("terraform-install-version"))
if err != nil {
return nil, fmt.Errorf("terraform-install-version must be a valid version: %w", err)
}
}

if config.ProviderVersion == "" {
return nil, fmt.Errorf("terraform-provider-version must be set")
}

// Validate flags
err := newFlagValidations().
err = newFlagValidations().
atLeastOne("grafana-url", "cloud-access-policy-token").
conflicting(
[]string{"grafana-url", "grafana-auth", "synthetic-monitoring-url", "synthetic-monitoring-access-token", "oncall-url", "oncall-access-token"},
Expand Down
73 changes: 73 additions & 0 deletions docs/data-sources/library_panels.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
---
# generated by https://github.com/hashicorp/terraform-plugin-docs
page_title: "grafana_library_panels Data Source - terraform-provider-grafana"
subcategory: "Grafana OSS"
description: |-
---

# grafana_library_panels (Data Source)



## Example Usage

```terraform
resource "grafana_library_panel" "test" {
name = "panelname"
model_json = jsonencode({
title = "test name"
type = "text"
version = 0
description = "test description"
})
}
resource "grafana_folder" "test" {
title = "Panel Folder"
uid = "panelname-folder"
}
resource "grafana_library_panel" "folder" {
name = "panelname In Folder"
folder_uid = grafana_folder.test.uid
model_json = jsonencode({
gridPos = {
x = 0
y = 0
h = 10
w = 10
}
title = "panel"
type = "text"
version = 0
})
}
data "grafana_library_panels" "all" {
depends_on = [grafana_library_panel.folder, grafana_library_panel.test]
}
```

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

### Optional

- `org_id` (String) The Organization ID. If not set, the default organization is used for basic authentication, or the one that owns your service account for token authentication.

### Read-Only

- `id` (String) The ID of this resource.
- `panels` (Set of Object) (see [below for nested schema](#nestedatt--panels))

<a id="nestedatt--panels"></a>
### Nested Schema for `panels`

Read-Only:

- `description` (String)
- `folder_uid` (String)
- `model_json` (String)
- `name` (String)
- `uid` (String)
2 changes: 1 addition & 1 deletion docs/resources/dashboard_permission_item.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ resource "grafana_dashboard_permission_item" "team" {

### Optional

- `org_id` (String) The Organization ID. If not set, the Org ID defined in the provider block will be used.
- `org_id` (String) The Organization ID. If not set, the default organization is used for basic authentication, or the one that owns your service account for token authentication.
- `role` (String) the role onto which the permission is to be assigned
- `team` (String) the team onto which the permission is to be assigned
- `user` (String) the user or service account onto which the permission is to be assigned
Expand Down
2 changes: 1 addition & 1 deletion docs/resources/data_source_permission_item.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ resource "grafana_data_source_permission_item" "service_account" {

### Optional

- `org_id` (String) The Organization ID. If not set, the Org ID defined in the provider block will be used.
- `org_id` (String) The Organization ID. If not set, the default organization is used for basic authentication, or the one that owns your service account for token authentication.
- `role` (String) the role onto which the permission is to be assigned
- `team` (String) the team onto which the permission is to be assigned
- `user` (String) the user or service account onto which the permission is to be assigned
Expand Down
2 changes: 1 addition & 1 deletion docs/resources/folder_permission_item.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ resource "grafana_folder_permission_item" "on_user" {

### Optional

- `org_id` (String) The Organization ID. If not set, the Org ID defined in the provider block will be used.
- `org_id` (String) The Organization ID. If not set, the default organization is used for basic authentication, or the one that owns your service account for token authentication.
- `role` (String) the role onto which the permission is to be assigned
- `team` (String) the team onto which the permission is to be assigned
- `user` (String) the user or service account onto which the permission is to be assigned
Expand Down
2 changes: 1 addition & 1 deletion docs/resources/role_assignment_item.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ resource "grafana_role_assignment_item" "service_account" {

### Optional

- `org_id` (String) The Organization ID. If not set, the Org ID defined in the provider block will be used.
- `org_id` (String) The Organization ID. If not set, the default organization is used for basic authentication, or the one that owns your service account for token authentication.
- `service_account_id` (String) the service account onto which the role is to be assigned
- `team_id` (String) the team onto which the role is to be assigned
- `user_id` (String) the user onto which the role is to be assigned
Expand Down
2 changes: 1 addition & 1 deletion docs/resources/service_account_permission_item.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ resource "grafana_service_account_permission_item" "on_user" {

### Optional

- `org_id` (String) The Organization ID. If not set, the Org ID defined in the provider block will be used.
- `org_id` (String) The Organization ID. If not set, the default organization is used for basic authentication, or the one that owns your service account for token authentication.
- `team` (String) the team onto which the permission is to be assigned
- `user` (String) the user or service account onto which the permission is to be assigned

Expand Down
34 changes: 34 additions & 0 deletions examples/data-sources/grafana_library_panels/data-source.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
resource "grafana_library_panel" "test" {
name = "panelname"
model_json = jsonencode({
title = "test name"
type = "text"
version = 0
description = "test description"
})
}

resource "grafana_folder" "test" {
title = "Panel Folder"
uid = "panelname-folder"
}

resource "grafana_library_panel" "folder" {
name = "panelname In Folder"
folder_uid = grafana_folder.test.uid
model_json = jsonencode({
gridPos = {
x = 0
y = 0
h = 10
w = 10
}
title = "panel"
type = "text"
version = 0
})
}

data "grafana_library_panels" "all" {
depends_on = [grafana_library_panel.folder, grafana_library_panel.test]
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ require (
google.golang.org/appengine v1.6.8 // indirect
google.golang.org/genproto/googleapis/api v0.0.0-20240415180920-8c6c420018be // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20240415180920-8c6c420018be // indirect
google.golang.org/grpc v1.64.0 // indirect
google.golang.org/grpc v1.64.1 // indirect
google.golang.org/protobuf v1.34.0 // indirect
gopkg.in/fsnotify/fsnotify.v1 v1.4.7 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
Expand Down
6 changes: 2 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -134,8 +134,6 @@ github.com/gopherjs/gopherjs v0.0.0-20181103185306-d547d1d9531e h1:JKmoR8x90Iww1
github.com/gopherjs/gopherjs v0.0.0-20181103185306-d547d1d9531e/go.mod h1:wJfORRmW1u3UXTncJ5qlYoELFm8eSnnEO6hX4iZ3EWY=
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.12-0.20240410110211-c9f68db085c4 h1:e7cZfDiNodjQn63be9m8zfnvMEQAMqHVFswjcbdlspk=
github.com/grafana/amixr-api-go-client v0.0.12-0.20240410110211-c9f68db085c4/go.mod h1:N6x26XUrM5zGtK5zL5vNJnAn2JFMxLFPPLTw/6pDkFE=
github.com/grafana/amixr-api-go-client v0.0.12 h1:oEHZTBhxoZ35EsfeccZBJGPKhZUVOmdSir3WWnSJMLc=
github.com/grafana/amixr-api-go-client v0.0.12/go.mod h1:N6x26XUrM5zGtK5zL5vNJnAn2JFMxLFPPLTw/6pDkFE=
github.com/grafana/grafana-com-public-clients/go/gcom v0.0.0-20240322153219-42c6a1d2bcab h1:/5R8NO996/keDkZqKXEkU3/QgFs1wzChKYkakjsBpRk=
Expand Down Expand Up @@ -513,8 +511,8 @@ google.golang.org/genproto/googleapis/api v0.0.0-20240415180920-8c6c420018be h1:
google.golang.org/genproto/googleapis/api v0.0.0-20240415180920-8c6c420018be/go.mod h1:dvdCTIoAGbkWbcIKBniID56/7XHTt6WfxXNMxuziJ+w=
google.golang.org/genproto/googleapis/rpc v0.0.0-20240415180920-8c6c420018be h1:LG9vZxsWGOmUKieR8wPAUR3u3MpnYFQZROPIMaXh7/A=
google.golang.org/genproto/googleapis/rpc v0.0.0-20240415180920-8c6c420018be/go.mod h1:WtryC6hu0hhx87FDGxWCDptyssuo68sk10vYjF+T9fY=
google.golang.org/grpc v1.64.0 h1:KH3VH9y/MgNQg1dE7b3XfVK0GsPSIzJwdF617gUSbvY=
google.golang.org/grpc v1.64.0/go.mod h1:oxjF8E3FBnjp+/gVFYdWacaLDx9na1aqy9oovLpxQYg=
google.golang.org/grpc v1.64.1 h1:LKtvyfbX3UGVPFcGqJ9ItpVWW6oN/2XqTxfAnwRRXiA=
google.golang.org/grpc v1.64.1/go.mod h1:hiQF4LFZelK2WKaP6W0L92zGHtiQdZxk8CrSdvyjeP0=
google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw=
google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc=
google.golang.org/protobuf v1.34.0 h1:Qo/qEd2RZPCf2nKuorzksSknv0d3ERwp1vFG38gSmH4=
Expand Down
46 changes: 45 additions & 1 deletion internal/resources/grafana/common_plugin_framework.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

goapi "github.com/grafana/grafana-openapi-client-go/client"
"github.com/grafana/terraform-provider-grafana/v3/internal/common"
"github.com/hashicorp/terraform-plugin-framework/datasource"
"github.com/hashicorp/terraform-plugin-framework/path"
"github.com/hashicorp/terraform-plugin-framework/resource"
frameworkSchema "github.com/hashicorp/terraform-plugin-framework/resource/schema"
Expand All @@ -15,6 +16,49 @@ import (
"github.com/hashicorp/terraform-plugin-framework/types"
)

type basePluginFrameworkDataSource struct {
client *goapi.GrafanaHTTPAPI
config *goapi.TransportConfig
}

func (r *basePluginFrameworkDataSource) Configure(ctx context.Context, req datasource.ConfigureRequest, resp *datasource.ConfigureResponse) {
// Configure is called multiple times (sometimes when ProviderData is not yet available), we only want to configure once
if req.ProviderData == nil || r.client != nil {
return
}

client, ok := req.ProviderData.(*common.Client)

if !ok {
resp.Diagnostics.AddError(
"Unexpected Resource Configure Type",
fmt.Sprintf("Expected *common.Client, got: %T. Please report this issue to the provider developers.", req.ProviderData),
)

return
}

r.client = client.GrafanaAPI
r.config = client.GrafanaAPIConfig
}

// clientFromNewOrgResource creates an OpenAPI client from the `org_id` attribute of a resource
// This client is meant to be used in `Create` functions when the ID hasn't already been baked into the resource ID
func (r *basePluginFrameworkDataSource) clientFromNewOrgResource(orgIDStr string) (*goapi.GrafanaHTTPAPI, int64, error) {
if r.client == nil {
return nil, 0, fmt.Errorf("client not configured")
}

client := r.client.Clone()
orgID, _ := strconv.ParseInt(orgIDStr, 10, 64)
if orgID == 0 {
orgID = client.OrgID()
} else if orgID > 0 {
client = client.WithOrgID(orgID)
}
return client, orgID, nil
}

type basePluginFrameworkResource struct {
client *goapi.GrafanaHTTPAPI
config *goapi.TransportConfig
Expand Down Expand Up @@ -98,7 +142,7 @@ func pluginFrameworkOrgIDAttribute() frameworkSchema.Attribute {
return frameworkSchema.StringAttribute{
Optional: true,
Computed: true,
Description: "The Organization ID. If not set, the Org ID defined in the provider block will be used.",
Description: "The Organization ID. If not set, the default organization is used for basic authentication, or the one that owns your service account for token authentication.",
PlanModifiers: []planmodifier.String{
stringplanmodifier.RequiresReplace(),
&orgIDAttributePlanModifier{},
Expand Down
Loading

0 comments on commit 9e32de6

Please sign in to comment.