Skip to content
Open
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
168 changes: 168 additions & 0 deletions mongodbatlas/data_source_mongodbatlas_cluster.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
package mongodbatlas

import (
"fmt"

ma "github.com/akshaykarle/go-mongodbatlas/mongodbatlas"
"github.com/hashicorp/terraform/helper/schema"
)

func dataSourceCluster() *schema.Resource {
return &schema.Resource{
Read: dataSourceClusterRead,

Schema: map[string]*schema.Schema{
"name": &schema.Schema{
Type: schema.TypeString,
Required: true,
},
"group": &schema.Schema{
Type: schema.TypeString,
Required: true,
},
"mongodb_major_version": &schema.Schema{
Type: schema.TypeString,
Computed: true,
},
"backup": &schema.Schema{
Type: schema.TypeBool,
Computed: true,
},
"provider_backup": &schema.Schema{
Type: schema.TypeBool,
Optional: true,
Computed: true,
},
"size": &schema.Schema{
Type: schema.TypeString,
Computed: true,
},
"provider_name": &schema.Schema{
Type: schema.TypeString,
Computed: true,
},
"backing_provider": &schema.Schema{
Type: schema.TypeString,
Computed: true,
},
"region": &schema.Schema{
Type: schema.TypeString,
Computed: true,
},
"disk_size_gb": &schema.Schema{
Type: schema.TypeFloat,
Computed: true,
},
"replication_factor": &schema.Schema{
Type: schema.TypeInt,
Computed: true,
},
"num_shards": &schema.Schema{
Type: schema.TypeInt,
Computed: true,
},
"paused": &schema.Schema{
Type: schema.TypeBool,
Computed: true,
},
"disk_gb_enabled": &schema.Schema{
Type: schema.TypeBool,
Computed: true,
},
"identifier": &schema.Schema{
Type: schema.TypeString,
Computed: true,
},
"state": &schema.Schema{
Type: schema.TypeString,
Computed: true,
},
"mongodb_version": &schema.Schema{
Type: schema.TypeString,
Computed: true,
},
"mongo_uri": &schema.Schema{
Type: schema.TypeString,
Computed: true,
},
"mongo_uri_updated": &schema.Schema{
Type: schema.TypeString,
Computed: true,
},
"mongo_uri_with_options": &schema.Schema{
Type: schema.TypeString,
Computed: true,
},
"replication_spec": &schema.Schema{
Type: schema.TypeSet,
Optional: true,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"region": {
Type: schema.TypeString,
Required: true,
},
"priority": {
Type: schema.TypeInt,
Required: true,
},
"electable_nodes": {
Type: schema.TypeInt,
Required: true,
},
"read_only_nodes": {
Type: schema.TypeInt,
Optional: true,
Default: 0,
},
},
},
},
},
}
}

func dataSourceClusterRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*ma.Client)
group := d.Get("group").(string)
name := d.Get("name").(string)
c, _, err := client.Clusters.Get(group, name)
if err != nil {
return fmt.Errorf("Error reading MongoDB Cluster with name %s: %s", name, err)
}
replicationSpecs := []interface{}{}
for region, replicationSpec := range c.ReplicationSpec {
spec := map[string]interface{}{
"region": region,
"priority": replicationSpec.Priority,
"electable_nodes": replicationSpec.ElectableNodes,
"read_only_nodes": replicationSpec.ReadOnlyNodes,
}
replicationSpecs = append(replicationSpecs, spec)
}

d.SetId(c.ID)
d.Set("name", c.Name)
d.Set("group", c.GroupID)
d.Set("mongodb_major_version", c.MongoDBMajorVersion)
d.Set("backup", c.BackupEnabled)
d.Set("provider_backup", c.ProviderBackupEnabled)
d.Set("size", c.ProviderSettings.InstanceSizeName)
d.Set("provider_name", c.ProviderSettings.ProviderName)
d.Set("backing_provider", c.ProviderSettings.BackingProviderName)
d.Set("region", c.ProviderSettings.RegionName)
d.Set("disk_size_gb", c.DiskSizeGB)
d.Set("disk_gb_enabled", c.AutoScaling.DiskGBEnabled)
d.Set("replication_factor", c.ReplicationFactor)
d.Set("state", c.StateName)
d.Set("num_shards", c.NumShards)
d.Set("paused", c.Paused)
d.Set("mongodb_version", c.MongoDBVersion)
d.Set("mongo_uri", c.MongoURI)
d.Set("mongo_uri_updated", c.MongoURIUpdated)
d.Set("mongo_uri_with_options", c.MongoURIWithOptions)
d.Set("replication_spec", replicationSpecs)

return nil
}
1 change: 1 addition & 0 deletions mongodbatlas/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ func Provider() terraform.ResourceProvider {
DataSourcesMap: map[string]*schema.Resource{
"mongodbatlas_project": dataSourceProject(),
"mongodbatlas_container": dataSourceContainer(),
"mongodbatlas_cluster": dataSourceCluster(),
},

ResourcesMap: map[string]*schema.Resource{
Expand Down
57 changes: 57 additions & 0 deletions website/docs/d/cluster.html.markdown
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
---
layout: "mongodbatlas"
page_title: "MongoDB Atlas: cluster"
sidebar_current: "docs-mongodbatlas-datasource-cluster"
description: |-
Provides details about a specific Cluster
---

# Data Source: mongodbatlas_cluster

`mongodbatlas_cluster` provides details about a specific Cluster.

This data source can prove useful when looking up the details of a previously created Cluster.

-> **NOTE:** Groups and projects are synonymous terms. `group` arguments on resources are the project ID.

## Example Usage

```hcl
data "mongodbatlas_project" "project" {
name = "my-project"
}

data "mongodbatlas_cluster" "example" {
group = "${data.mongodbatlas_project.project.id}"
name = "my-cluster-name"
}
```

## Argument Reference

* `group` - (Required) The ID of the project that the desired cluster belongs to.
* `identifier` - (Required) The name of the desired cluster.

## Attributes Reference

In addition to all arguments above, the following attributes are exported:

* `id` - The same as `identifier`.
* `mongodb_major_version` - Version of the cluster.
* `backup` - If continuous backups are enabled.
* `provider_backup` - If cloud provider snapshots are enabled.
* `size` - Instance size of all data-bearing servers in the cluster.
* `provider_name` - Name of the cloud provider. One of: `AWS`, `GCP`, `AZURE`, `TENANT`.
* `backing_provider` - The cloud service provider for a shared tier cluster. One of `AWS`, `GCP`, `AZURE`.
* `region` - Atlas-style name of the region in which the cluster was created. e.g. `US_EAST_1`.
* `disk_size_gb` - AWS/GCP only. Size in GB of the server's root volume.
* `disk_gb_enabled` - If disk auto-scaling is enabled.
* `replication_factor` - Number of replica set members. Each shard is a replica set with the specified replication factor if a sharded cluster.
* `state` - Current state of the cluster. One of: `IDLE`, `CREATING`,
`UPDATING`, `DELETING`, `DELETED`, `REPAIRING`.
* `num_shards` - The number of active shards.
* `paused` - Flag that indicates whether the cluster is paused.
* `mongodb_version` - Version of MongoDB deployed. Major.Minor.Patch.
* `mongo_uri` - Base connection string for the cluster. See `mongo_uri_with_options` for a more usable connection string.
* `mongo_uri_updated` - When the connection string was last updated. Connection string changes, for example, if you change a replica set to a sharded cluster.
* `mongo_uri_with_options` - Connection string for connecting to the Atlas cluster. Includes necessary query parameters with values appropriate for the cluster. Include a username and password for a MongoDB user associated with the project after the `mongodb://` to actually connect.