Skip to content

Project data source #142

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

Merged
merged 5 commits into from
Feb 20, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
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
37 changes: 28 additions & 9 deletions mongodbatlas/data_source_mongodbatlas_project.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package mongodbatlas

import (
"context"
"errors"
"fmt"

"github.com/hashicorp/terraform/helper/schema"
Expand All @@ -14,8 +15,14 @@ func dataSourceMongoDBAtlasProject() *schema.Resource {
Read: dataSourceMongoDBAtlasProjectRead,
Schema: map[string]*schema.Schema{
"project_id": {
Type: schema.TypeString,
Required: true,
Type: schema.TypeString,
Optional: true,
ConflictsWith: []string{"name"},
},
"name": {
Type: schema.TypeString,
Optional: true,
ConflictsWith: []string{"project_id"},
},
"org_id": {
Type: schema.TypeString,
Expand Down Expand Up @@ -55,29 +62,41 @@ func dataSourceMongoDBAtlasProject() *schema.Resource {
func dataSourceMongoDBAtlasProjectRead(d *schema.ResourceData, meta interface{}) error {
//Get client connection.
conn := meta.(*matlas.Client)
projectID := d.Get("project_id").(string)

project, _, err := conn.Projects.GetOneProject(context.Background(), projectID)
projectID, projectIDOk := d.GetOk("project_id")
name, nameOk := d.GetOk("name")
if !projectIDOk && !nameOk {
return errors.New("either project_id or name must be configured")
}

var err error
var project *matlas.Project

if projectIDOk {
project, _, err = conn.Projects.GetOneProject(context.Background(), projectID.(string))
} else {
project, _, err = conn.Projects.GetOneProjectByName(context.Background(), name.(string))
}
if err != nil {
return fmt.Errorf(errorProjectRead, projectID, err)
}

teams, _, err := conn.Projects.GetProjectTeamsAssigned(context.Background(), projectID)
teams, _, err := conn.Projects.GetProjectTeamsAssigned(context.Background(), project.ID)
if err != nil {
return fmt.Errorf("error getting project's teams assigned (%s): %s", projectID, err)
}

if err := d.Set("org_id", project.OrgID); err != nil {
return fmt.Errorf(errorProjectSetting, `org_id`, projectID, err)
return fmt.Errorf(errorProjectSetting, `org_id`, project.ID, err)
}
if err := d.Set("cluster_count", project.ClusterCount); err != nil {
return fmt.Errorf(errorProjectSetting, `clusterCount`, projectID, err)
return fmt.Errorf(errorProjectSetting, `clusterCount`, project.ID, err)
}
if err := d.Set("created", project.Created); err != nil {
return fmt.Errorf(errorProjectSetting, `created`, projectID, err)
return fmt.Errorf(errorProjectSetting, `created`, project.ID, err)
}
if err := d.Set("teams", flattenTeams(teams)); err != nil {
return fmt.Errorf(errorProjectSetting, `teams`, projectID, err)
return fmt.Errorf(errorProjectSetting, `teams`, project.ID, err)
}

d.SetId(project.ID)
Expand Down
47 changes: 44 additions & 3 deletions mongodbatlas/data_source_mongodbatlas_project_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
matlas "github.com/mongodb/go-client-mongodb-atlas/mongodbatlas"
)

func TestAccDataSourceMongoDBAtlasProject_basic(t *testing.T) {
func TestAccDataSourceMongoDBAtlasProject_byID(t *testing.T) {

projectName := fmt.Sprintf("test-datasource-project-%s", acctest.RandString(10))
orgID := os.Getenv("MONGODB_ATLAS_ORG_ID")
Expand All @@ -20,7 +20,7 @@ func TestAccDataSourceMongoDBAtlasProject_basic(t *testing.T) {
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccMongoDBAtlasProjectConfigWithDS(projectName, orgID,
Config: testAccMongoDBAtlasProjectConfigWithDSByID(projectName, orgID,
[]*matlas.ProjectTeam{
{
TeamID: "5e0fa8c99ccf641c722fe683",
Expand All @@ -41,7 +41,38 @@ func TestAccDataSourceMongoDBAtlasProject_basic(t *testing.T) {
})
}

func testAccMongoDBAtlasProjectConfigWithDS(projectName, orgID string, teams []*matlas.ProjectTeam) string {
func TestAccDataSourceMongoDBAtlasProject_byName(t *testing.T) {

projectName := fmt.Sprintf("test-datasource-project-%s", acctest.RandString(10))
orgID := os.Getenv("MONGODB_ATLAS_ORG_ID")

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccMongoDBAtlasProjectConfigWithDSByName(projectName, orgID,
[]*matlas.ProjectTeam{
{
TeamID: "5e0fa8c99ccf641c722fe683",
RoleNames: []string{"GROUP_READ_ONLY", "GROUP_DATA_ACCESS_ADMIN"},
},
{
TeamID: "5e1dd7b4f2a30ba80a70cd3a",
RoleNames: []string{"GROUP_DATA_ACCESS_ADMIN", "GROUP_OWNER"},
},
},
),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttrSet("mongodbatlas_project.test", "name"),
resource.TestCheckResourceAttrSet("mongodbatlas_project.test", "org_id"),
),
},
},
})
}

func testAccMongoDBAtlasProjectConfigWithDSByID(projectName, orgID string, teams []*matlas.ProjectTeam) string {
return fmt.Sprintf(`
%s

Expand All @@ -50,3 +81,13 @@ func testAccMongoDBAtlasProjectConfigWithDS(projectName, orgID string, teams []*
}
`, testAccMongoDBAtlasPropjectConfig(projectName, orgID, teams))
}

func testAccMongoDBAtlasProjectConfigWithDSByName(projectName, orgID string, teams []*matlas.ProjectTeam) string {
return fmt.Sprintf(`
%s

data "mongodbatlas_project" "test" {
name = "${mongodbatlas_project.test.name}"
}
`, testAccMongoDBAtlasPropjectConfig(projectName, orgID, teams))
}
28 changes: 27 additions & 1 deletion website/docs/d/project.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ description: |-

## Example Usage

### Using project_id attribute to query
```hcl
resource "mongodbatlas_project" "test" {
name = "project-name"
Expand All @@ -35,9 +36,34 @@ data "mongodbatlas_project" "test" {
}
```

### Using name attribute to query
```hcl
resource "mongodbatlas_project" "test" {
name = "project-name"
org_id = "<ORG_ID>"

teams {
team_id = "5e0fa8c99ccf641c722fe645"
role_names = ["GROUP_OWNER"]

}
teams {
team_id = "5e1dd7b4f2a30ba80a70cd4rw"
role_names = ["GROUP_READ_ONLY", "GROUP_DATA_ACCESS_READ_WRITE"]
}
}

data "mongodbatlas_project" "test" {
name = "${mongodbatlas_project.test.name}"
}
```

## Argument Reference

* `project_id` - The unique ID for the project.
* `project_id` - (Optional) The unique ID for the project.
* `name` - (Optional) The unique ID for the project.

~> **IMPORTANT:** Either `project_id` or `name` must be configurated.

## Attributes Reference

Expand Down