Skip to content
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

WIP: Implement looker_project and more #15

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
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*
7 changes: 7 additions & 0 deletions examples/resources/looker_project/resource.tf
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}"
}
20 changes: 20 additions & 0 deletions examples/resources/looker_project_git_deploy_key/resource.tf
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
}

24 changes: 24 additions & 0 deletions examples/resources/looker_project_git_repository/resource.tf
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 = "git@github.com: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
}

27 changes: 15 additions & 12 deletions pkg/looker/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,19 +51,22 @@ func Provider() *schema.Provider {
},
},
ResourcesMap: map[string]*schema.Resource{
"looker_user": resourceUser(),
"looker_user_roles": resourceUserRoles(),
"looker_permission_set": resourcePermissionSet(),
"looker_model_set": resourceModelSet(),
"looker_group": resourceGroup(),
"looker_group_membership": resourceGroupMembership(),
"looker_role": resourceRole(),
"looker_role_groups": resourceRoleGroups(),
"looker_user_attribute": resourceUserAttribute(),
"looker_user_attribute_user_value": resourceUserAttributeUserValue(),
"looker_user": resourceUser(),
"looker_user_roles": resourceUserRoles(),
"looker_permission_set": resourcePermissionSet(),
"looker_model_set": resourceModelSet(),
"looker_group": resourceGroup(),
"looker_group_membership": resourceGroupMembership(),
"looker_role": resourceRole(),
"looker_role_groups": resourceRoleGroups(),
"looker_user_attribute": resourceUserAttribute(),
"looker_user_attribute_user_value": resourceUserAttributeUserValue(),
"looker_user_attribute_group_value": resourceUserAttributeGroupValue(),
"looker_connection": resourceConnection(),
"looker_lookml_model": resourceLookMLModel(),
"looker_connection": resourceConnection(),
"looker_lookml_model": resourceLookMLModel(),
"looker_project": resourceProject(),
"looker_project_git_deploy_key": resourceProjectGitDeployKey(),
"looker_project_git_repository": resourceProjectGitRepository(),
},

ConfigureContextFunc: providerConfigure,
Expand Down
82 changes: 82 additions & 0 deletions pkg/looker/resource_project.go
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)
Copy link
Owner

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

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
}

79 changes: 79 additions & 0 deletions pkg/looker/resource_project_git_deploy_key.go
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)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should check err like below:

Suggested change
d.Set("git_deploy_key", gitDeployKey)
if err = d.Set("git_deploy_key", gitDeployKey); err != nil {
return diag.FromErr(err)
}


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)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
d.Set("git_deploy_key", gitDeployKey)
if err = d.Set("git_deploy_key", gitDeployKey); err != nil {
return diag.FromErr(err)
}


return nil
}

func resourceProjectGitDeployKeyDelete(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
d.SetId("")
return nil
}

120 changes: 120 additions & 0 deletions pkg/looker/resource_project_git_repository.go
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)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
d.Set("git_service_name", project.GitServiceName)
if err = d.Set("git_service_name", project.GitServiceName); err != nil {
return diag.FromErr(err)
}

d.Set("git_remote_url", project.GitRemoteUrl)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
d.Set("git_remote_url", project.GitRemoteUrl)
if err = d.Set("git_remote_url", project.GitRemoteUrl); err != nil {
return diag.FromErr(err)
}


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
}

Loading