-
Notifications
You must be signed in to change notification settings - Fork 15
WIP: Implement looker_project and more #15
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,11 @@ | ||
.terraform | ||
terraform-provider-looker | ||
.env | ||
.envrc | ||
crash.log | ||
terraform.tfstate | ||
terraform.tfstate.backup | ||
bin | ||
vendor | ||
build | ||
examples/**/*.terraform* |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
resource "random_id" "this" { | ||
byte_length = 2 | ||
} | ||
|
||
resource "looker_project" "this" { | ||
name = "tf_test_${random_id.this.hex}" | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
resource "random_id" "this" { | ||
byte_length = 2 | ||
} | ||
|
||
resource "looker_project" "this" { | ||
name = "tf_test_${random_id.this.hex}" | ||
} | ||
|
||
resource "looker_project_git_deploy_key" "this" { | ||
project = looker_project.this.id | ||
} | ||
|
||
output "looker_project" { | ||
value = looker_project.this | ||
} | ||
|
||
output "project_git_deploy_key" { | ||
value = looker_project_git_deploy_key.this | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
resource "random_id" "this" { | ||
byte_length = 2 | ||
} | ||
|
||
resource "looker_project" "this" { | ||
name = "tf_test_${random_id.this.hex}" | ||
} | ||
|
||
resource "looker_project_git_deploy_key" "this" { | ||
project = looker_project.this.id | ||
} | ||
|
||
resource "looker_project_git_repository" "this" { | ||
project = looker_project.this.name | ||
git_service_name = "github" | ||
git_remote_url = "[email protected]:puc-business-intelligence/looker_project_tf_test_123.git" | ||
|
||
depends_on = [looker_project_git_deploy_key.this] | ||
} | ||
|
||
output "looker_project_git_repository" { | ||
value = looker_project_git_repository.this | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
package looker | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/hashicorp/terraform-plugin-sdk/v2/diag" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation" | ||
apiclient "github.com/looker-open-source/sdk-codegen/go/sdk/v4" | ||
) | ||
|
||
func resourceProject() *schema.Resource { | ||
return &schema.Resource{ | ||
CreateContext: resourceProjectCreate, | ||
ReadContext: resourceProjectRead, | ||
DeleteContext: resourceProjectDelete, | ||
Importer: &schema.ResourceImporter{ | ||
StateContext: schema.ImportStatePassthroughContext, | ||
}, | ||
|
||
Schema: map[string]*schema.Schema{ | ||
"name": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
ForceNew: true, | ||
ValidateFunc: validation.StringDoesNotContainAny(" "), | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func resourceProjectCreate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { | ||
client := m.(*apiclient.LookerSDK) | ||
// Set client to dev workspace | ||
if err := updateApiSessionWorkspaceId(client, "dev"); err != nil { | ||
return diag.FromErr(err) | ||
} | ||
|
||
// Create the project | ||
projectName := d.Get("name").(string) | ||
writeProject := apiclient.WriteProject{ | ||
Name: &projectName, | ||
} | ||
project, err := client.CreateProject(writeProject, nil) | ||
if err != nil { | ||
return diag.FromErr(err) | ||
} | ||
d.SetId(*project.Id) | ||
|
||
return nil | ||
} | ||
|
||
func resourceProjectRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { | ||
client := m.(*apiclient.LookerSDK) | ||
// Set client to dev workspace | ||
if err := updateApiSessionWorkspaceId(client, "dev"); err != nil { | ||
return diag.FromErr(err) | ||
} | ||
|
||
project, err := client.Project(d.Id(), "name", nil) | ||
if err != nil { | ||
return diag.FromErr(err) | ||
} | ||
|
||
if err = d.Set("name", project.Name); err != nil { | ||
return diag.FromErr(err) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func resourceProjectDelete(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { | ||
var diags diag.Diagnostics | ||
diags = append(diags, diag.Diagnostic{ | ||
Severity: diag.Warning, | ||
Summary: "Deleting a Looker project is currently impossible via the API.", | ||
Detail: "In order to delete a Looker project, and to avoid naming conflicts the usage of random_id is advised.\nSee also: https://docs.looker.com/data-modeling/getting-started/manage-projects#deleting_a_project", | ||
}) | ||
d.SetId("") | ||
return diags | ||
} | ||
|
Original file line number | Diff line number | Diff line change | ||||||||
---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,79 @@ | ||||||||||
package looker | ||||||||||
|
||||||||||
import ( | ||||||||||
"context" | ||||||||||
"strings" | ||||||||||
|
||||||||||
"github.com/hashicorp/terraform-plugin-sdk/v2/diag" | ||||||||||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" | ||||||||||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation" | ||||||||||
apiclient "github.com/looker-open-source/sdk-codegen/go/sdk/v4" | ||||||||||
) | ||||||||||
|
||||||||||
func resourceProjectGitDeployKey() *schema.Resource { | ||||||||||
return &schema.Resource{ | ||||||||||
CreateContext: resourceProjectGitDeployKeyCreate, | ||||||||||
ReadContext: resourceProjectGitDeployKeyRead, | ||||||||||
DeleteContext: resourceProjectGitDeployKeyDelete, | ||||||||||
Importer: &schema.ResourceImporter{ | ||||||||||
StateContext: schema.ImportStatePassthroughContext, | ||||||||||
}, | ||||||||||
|
||||||||||
Schema: map[string]*schema.Schema{ | ||||||||||
"project": { | ||||||||||
Type: schema.TypeString, | ||||||||||
Required: true, | ||||||||||
ForceNew: true, | ||||||||||
ValidateFunc: validation.StringDoesNotContainAny(" "), | ||||||||||
}, | ||||||||||
"git_deploy_key": { | ||||||||||
Type: schema.TypeString, | ||||||||||
Computed: true, | ||||||||||
}, | ||||||||||
}, | ||||||||||
} | ||||||||||
} | ||||||||||
|
||||||||||
func resourceProjectGitDeployKeyCreate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { | ||||||||||
client := m.(*apiclient.LookerSDK) | ||||||||||
// Set client to dev workspace | ||||||||||
if err := updateApiSessionWorkspaceId(client, "dev"); err != nil { | ||||||||||
return diag.FromErr(err) | ||||||||||
} | ||||||||||
|
||||||||||
projectName := d.Get("project").(string) | ||||||||||
d.SetId(projectName) | ||||||||||
gitDeployKey, err := client.CreateGitDeployKey(projectName, nil) | ||||||||||
if err != nil { | ||||||||||
// Graceful fallback if there is already a git deploy key setup | ||||||||||
if strings.Contains(err.Error(), "409") { | ||||||||||
return resourceProjectGitDeployKeyRead(ctx, d, m) | ||||||||||
} | ||||||||||
return diag.FromErr(err) | ||||||||||
} | ||||||||||
d.Set("git_deploy_key", gitDeployKey) | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. should check
Suggested change
|
||||||||||
|
||||||||||
return nil | ||||||||||
} | ||||||||||
|
||||||||||
func resourceProjectGitDeployKeyRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { | ||||||||||
client := m.(*apiclient.LookerSDK) | ||||||||||
// Set client to dev workspace | ||||||||||
if err := updateApiSessionWorkspaceId(client, "dev"); err != nil { | ||||||||||
return diag.FromErr(err) | ||||||||||
} | ||||||||||
|
||||||||||
gitDeployKey, err := client.GitDeployKey(d.Id(), nil) | ||||||||||
if err != nil { | ||||||||||
return diag.FromErr(err) | ||||||||||
} | ||||||||||
d.Set("git_deploy_key", gitDeployKey) | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||
|
||||||||||
return nil | ||||||||||
} | ||||||||||
|
||||||||||
func resourceProjectGitDeployKeyDelete(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { | ||||||||||
d.SetId("") | ||||||||||
return nil | ||||||||||
} | ||||||||||
|
Original file line number | Diff line number | Diff line change | ||||||||
---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,120 @@ | ||||||||||
package looker | ||||||||||
|
||||||||||
import ( | ||||||||||
"context" | ||||||||||
|
||||||||||
"github.com/hashicorp/terraform-plugin-sdk/v2/diag" | ||||||||||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" | ||||||||||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation" | ||||||||||
apiclient "github.com/looker-open-source/sdk-codegen/go/sdk/v4" | ||||||||||
) | ||||||||||
|
||||||||||
func resourceProjectGitRepository() *schema.Resource { | ||||||||||
return &schema.Resource{ | ||||||||||
CreateContext: resourceProjectGitRepositoryCreate, | ||||||||||
ReadContext: resourceProjectGitRepositoryRead, | ||||||||||
UpdateContext: resourceProjectGitRepositoryUpdate, | ||||||||||
DeleteContext: resourceProjectGitRepositoryDelete, | ||||||||||
Importer: &schema.ResourceImporter{ | ||||||||||
StateContext: schema.ImportStatePassthroughContext, | ||||||||||
}, | ||||||||||
|
||||||||||
Schema: map[string]*schema.Schema{ | ||||||||||
"project": { | ||||||||||
Type: schema.TypeString, | ||||||||||
Required: true, | ||||||||||
ForceNew: true, | ||||||||||
ValidateFunc: validation.StringDoesNotContainAny(" "), | ||||||||||
}, | ||||||||||
"git_service_name": { | ||||||||||
Type: schema.TypeString, | ||||||||||
Optional: true, | ||||||||||
Default: "bare", | ||||||||||
ForceNew: true, | ||||||||||
ValidateFunc: validation.StringInSlice([]string{"bare", "github"}, false), | ||||||||||
}, | ||||||||||
"git_remote_url": { | ||||||||||
Type: schema.TypeString, | ||||||||||
Optional: true, | ||||||||||
}, | ||||||||||
"git_deploy_key": { | ||||||||||
Type: schema.TypeString, | ||||||||||
Computed: true, | ||||||||||
}, | ||||||||||
}, | ||||||||||
} | ||||||||||
} | ||||||||||
|
||||||||||
func resourceProjectGitRepositoryCreate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { | ||||||||||
client := m.(*apiclient.LookerSDK) | ||||||||||
// Set client to dev workspace | ||||||||||
if err := updateApiSessionWorkspaceId(client, "dev"); err != nil { | ||||||||||
return diag.FromErr(err) | ||||||||||
} | ||||||||||
|
||||||||||
// Update the project git service name | ||||||||||
projectName := d.Get("project").(string) | ||||||||||
d.SetId(projectName) | ||||||||||
gitServiceName := d.Get("git_service_name").(string) | ||||||||||
var gitRemoteUrl string | ||||||||||
if gitServiceName != "bare" { | ||||||||||
gitRemoteUrl = d.Get("git_remote_url").(string) | ||||||||||
} | ||||||||||
updateProject := apiclient.WriteProject{ | ||||||||||
GitServiceName: &gitServiceName, | ||||||||||
GitRemoteUrl: &gitRemoteUrl, | ||||||||||
} | ||||||||||
_, err := client.UpdateProject(projectName, updateProject, "name,git_service_name,git_remote_url", nil) | ||||||||||
if err != nil { | ||||||||||
return diag.FromErr(err) | ||||||||||
} | ||||||||||
|
||||||||||
return resourceProjectGitRepositoryRead(ctx, d, m) | ||||||||||
} | ||||||||||
|
||||||||||
func resourceProjectGitRepositoryRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { | ||||||||||
client := m.(*apiclient.LookerSDK) | ||||||||||
// Set client to dev workspace | ||||||||||
if err := updateApiSessionWorkspaceId(client, "dev"); err != nil { | ||||||||||
return diag.FromErr(err) | ||||||||||
} | ||||||||||
|
||||||||||
project, err := client.Project(d.Id(), "name,git_service_name,git_remote_url", nil) | ||||||||||
if err != nil { | ||||||||||
return diag.FromErr(err) | ||||||||||
} | ||||||||||
d.Set("git_service_name", project.GitServiceName) | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||
d.Set("git_remote_url", project.GitRemoteUrl) | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||
|
||||||||||
return nil | ||||||||||
} | ||||||||||
|
||||||||||
func resourceProjectGitRepositoryUpdate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { | ||||||||||
client := m.(*apiclient.LookerSDK) | ||||||||||
// Set client to dev workspace | ||||||||||
if err := updateApiSessionWorkspaceId(client, "dev"); err != nil { | ||||||||||
return diag.FromErr(err) | ||||||||||
} | ||||||||||
|
||||||||||
gitServiceName := d.Get("git_service_name").(string) | ||||||||||
var gitRemoteUrl string | ||||||||||
if gitServiceName != "bare" { | ||||||||||
gitRemoteUrl = d.Get("git_remote_url").(string) | ||||||||||
} | ||||||||||
writeProject := apiclient.WriteProject{ | ||||||||||
GitServiceName: &gitServiceName, | ||||||||||
GitRemoteUrl: &gitRemoteUrl, | ||||||||||
} | ||||||||||
_, err := client.UpdateProject(d.Id(), writeProject, "name,git_service_name,git_remote_url", nil) | ||||||||||
if err != nil { | ||||||||||
return diag.FromErr(err) | ||||||||||
} | ||||||||||
|
||||||||||
return nil | ||||||||||
} | ||||||||||
|
||||||||||
func resourceProjectGitRepositoryDelete(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { | ||||||||||
d.SetId("") | ||||||||||
return nil | ||||||||||
} | ||||||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Cloud you add
[DEBUG]
log before api call in Create, Read, Update, and Delete?For example,
https://github.com/hirosassa/terraform-provider-looker/blob/master/pkg/looker/resource_user_attribute.go#L75