Skip to content

chore: Explores nested models by generating the stream_connection_api resource #3372

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

Closed
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
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ jira-release-version: ## Update Jira version in a release

.PHONY: enable-autogen
enable-autogen: ## Enable use of autogen resources in the provider
make add-lines filename=./internal/provider/provider.go find="project.Resource," add="customdbroleapi.Resource,databaseuserapi.Resource,pushbasedlogexportapi.Resource,searchdeploymentapi.Resource,projectapi.Resource,resourcepolicyapi.Resource,"
make add-lines filename=./internal/provider/provider.go find="project.Resource," add="customdbroleapi.Resource,databaseuserapi.Resource,pushbasedlogexportapi.Resource,searchdeploymentapi.Resource,projectapi.Resource,resourcepolicyapi.Resource,streamconnectionapi.Resource,"

.PHONY: delete-lines ${filename} ${delete}
delete-lines:
Expand Down
248 changes: 248 additions & 0 deletions docs/resources/stream_connection_api.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,248 @@
# Resource: mongodbatlas_stream_connection_api

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

### Required

- `group_id` (String) Unique 24-hexadecimal digit string that identifies your project. Use the [/groups](#tag/Projects/operation/listProjects) endpoint to retrieve all projects to which the authenticated user has access.

**NOTE**: Groups and projects are synonymous terms. Your group id is the same as your project id. For existing groups, your group/project id remains the same. The resource and corresponding endpoints use the term groups.
- `tenant_name` (String) Human-readable label that identifies the stream instance.

### Optional

- `name` (String) Human-readable label that identifies the stream connection. In the case of the Sample type, this is the name of the sample source.
- `type_awslambda` (Attributes) (see [below for nested schema](#nestedatt--type_awslambda))
- `type_cluster` (Attributes) (see [below for nested schema](#nestedatt--type_cluster))
- `type_https` (Attributes) (see [below for nested schema](#nestedatt--type_https))
- `type_kafka` (Attributes) (see [below for nested schema](#nestedatt--type_kafka))
- `type_s3` (Attributes) (see [below for nested schema](#nestedatt--type_s3))
- `type_sample` (Attributes) (see [below for nested schema](#nestedatt--type_sample))

### Read-Only

- `links` (Attributes List) List of one or more Uniform Resource Locators (URLs) that point to API sub-resources, related API resources, or both. RFC 5988 outlines these relationships. (see [below for nested schema](#nestedatt--links))
- `type` (String) Type of the connection.

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

Optional:

- `aws` (Attributes) AWS configurations for AWS-based connection types. (see [below for nested schema](#nestedatt--type_awslambda--aws))

<a id="nestedatt--type_awslambda--aws"></a>
### Nested Schema for `type_awslambda.aws`

Optional:

- `role_arn` (String) Amazon Resource Name (ARN) that identifies the Amazon Web Services (AWS) Identity and Access Management (IAM) role that MongoDB Cloud assumes when it accesses resources in your AWS account.
- `test_bucket` (String) The name of an S3 bucket used to check authorization of the passed-in IAM role ARN.

Read-Only:

- `links` (Attributes List) List of one or more Uniform Resource Locators (URLs) that point to API sub-resources, related API resources, or both. RFC 5988 outlines these relationships. (see [below for nested schema](#nestedatt--type_awslambda--aws--links))

<a id="nestedatt--type_awslambda--aws--links"></a>
### Nested Schema for `type_awslambda.aws.links`

Read-Only:

- `href` (String) Uniform Resource Locator (URL) that points another API resource to which this response has some relationship. This URL often begins with `https://cloud.mongodb.com/api/atlas`.
- `rel` (String) Uniform Resource Locator (URL) that defines the semantic relationship between this resource and another API resource. This URL often begins with `https://cloud.mongodb.com/api/atlas`.




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

Optional:

- `cluster_name` (String) Name of the cluster configured for this connection.
- `db_role_to_execute` (Attributes) The name of a Built in or Custom DB Role to connect to an Atlas Cluster. (see [below for nested schema](#nestedatt--type_cluster--db_role_to_execute))

<a id="nestedatt--type_cluster--db_role_to_execute"></a>
### Nested Schema for `type_cluster.db_role_to_execute`

Optional:

- `role` (String) The name of the role to use. Can be a built in role or a custom role.
- `type` (String) Type of the DB role. Can be either BuiltIn or Custom.

Read-Only:

- `links` (Attributes List) List of one or more Uniform Resource Locators (URLs) that point to API sub-resources, related API resources, or both. RFC 5988 outlines these relationships. (see [below for nested schema](#nestedatt--type_cluster--db_role_to_execute--links))

<a id="nestedatt--type_cluster--db_role_to_execute--links"></a>
### Nested Schema for `type_cluster.db_role_to_execute.links`

Read-Only:

- `href` (String) Uniform Resource Locator (URL) that points another API resource to which this response has some relationship. This URL often begins with `https://cloud.mongodb.com/api/atlas`.
- `rel` (String) Uniform Resource Locator (URL) that defines the semantic relationship between this resource and another API resource. This URL often begins with `https://cloud.mongodb.com/api/atlas`.




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

Optional:

- `headers` (Map of String) A map of key-value pairs that will be passed as headers for the request.
- `url` (String) The url to be used for the request.


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

Optional:

- `authentication` (Attributes) User credentials required to connect to a Kafka Cluster. Includes the authentication type, as well as the parameters for that authentication mode. (see [below for nested schema](#nestedatt--type_kafka--authentication))
- `bootstrap_servers` (String) Comma separated list of server addresses.
- `config` (Map of String) A map of Kafka key-value pairs for optional configuration. This is a flat object, and keys can have '.' characters.
- `networking` (Attributes) Networking Access Type can either be 'PUBLIC' (default) or VPC. VPC type is in public preview, please file a support ticket to enable VPC Network Access. (see [below for nested schema](#nestedatt--type_kafka--networking))
- `security` (Attributes) Properties for the secure transport connection to Kafka. For SSL, this can include the trusted certificate to use. (see [below for nested schema](#nestedatt--type_kafka--security))

<a id="nestedatt--type_kafka--authentication"></a>
### Nested Schema for `type_kafka.authentication`

Optional:

- `mechanism` (String) Style of authentication. Can be one of PLAIN, SCRAM-256, or SCRAM-512.
- `password` (String, Sensitive) Password of the account to connect to the Kafka cluster.
- `ssl_certificate` (String) SSL certificate for client authentication to Kafka.
- `ssl_key` (String) SSL key for client authentication to Kafka.
- `ssl_key_password` (String) Password for the SSL key, if it is password protected.
- `username` (String) Username of the account to connect to the Kafka cluster.

Read-Only:

- `links` (Attributes List) List of one or more Uniform Resource Locators (URLs) that point to API sub-resources, related API resources, or both. RFC 5988 outlines these relationships. (see [below for nested schema](#nestedatt--type_kafka--authentication--links))

<a id="nestedatt--type_kafka--authentication--links"></a>
### Nested Schema for `type_kafka.authentication.links`

Read-Only:

- `href` (String) Uniform Resource Locator (URL) that points another API resource to which this response has some relationship. This URL often begins with `https://cloud.mongodb.com/api/atlas`.
- `rel` (String) Uniform Resource Locator (URL) that defines the semantic relationship between this resource and another API resource. This URL often begins with `https://cloud.mongodb.com/api/atlas`.



<a id="nestedatt--type_kafka--networking"></a>
### Nested Schema for `type_kafka.networking`

Optional:

- `access` (Attributes) Information about the networking access. (see [below for nested schema](#nestedatt--type_kafka--networking--access))

Read-Only:

- `links` (Attributes List) List of one or more Uniform Resource Locators (URLs) that point to API sub-resources, related API resources, or both. RFC 5988 outlines these relationships. (see [below for nested schema](#nestedatt--type_kafka--networking--links))

<a id="nestedatt--type_kafka--networking--access"></a>
### Nested Schema for `type_kafka.networking.access`

Optional:

- `connection_id` (String) Reserved. Will be used by PRIVATE_LINK connection type.
- `name` (String) Reserved. Will be used by PRIVATE_LINK connection type.
- `tgw_id` (String) Reserved. Will be used by TRANSIT_GATEWAY connection type.
- `type` (String) Selected networking type. Either PUBLIC, VPC, PRIVATE_LINK, or TRANSIT_GATEWAY. Defaults to PUBLIC. For VPC, ensure that VPC peering exists and connectivity has been established between Atlas VPC and the VPC where Kafka cluster is hosted for the connection to function properly. TRANSIT_GATEWAY support is coming soon.
- `vpc_cidr` (String) Reserved. Will be used by TRANSIT_GATEWAY connection type.

Read-Only:

- `links` (Attributes List) List of one or more Uniform Resource Locators (URLs) that point to API sub-resources, related API resources, or both. RFC 5988 outlines these relationships. (see [below for nested schema](#nestedatt--type_kafka--networking--access--links))

<a id="nestedatt--type_kafka--networking--access--links"></a>
### Nested Schema for `type_kafka.networking.access.links`

Read-Only:

- `href` (String) Uniform Resource Locator (URL) that points another API resource to which this response has some relationship. This URL often begins with `https://cloud.mongodb.com/api/atlas`.
- `rel` (String) Uniform Resource Locator (URL) that defines the semantic relationship between this resource and another API resource. This URL often begins with `https://cloud.mongodb.com/api/atlas`.



<a id="nestedatt--type_kafka--networking--links"></a>
### Nested Schema for `type_kafka.networking.links`

Read-Only:

- `href` (String) Uniform Resource Locator (URL) that points another API resource to which this response has some relationship. This URL often begins with `https://cloud.mongodb.com/api/atlas`.
- `rel` (String) Uniform Resource Locator (URL) that defines the semantic relationship between this resource and another API resource. This URL often begins with `https://cloud.mongodb.com/api/atlas`.



<a id="nestedatt--type_kafka--security"></a>
### Nested Schema for `type_kafka.security`

Optional:

- `broker_public_certificate` (String) A trusted, public x509 certificate for connecting to Kafka over SSL.
- `protocol` (String) Describes the transport type. Can be either SASL_PLAINTEXT, SASL_SSL, or SSL.

Read-Only:

- `links` (Attributes List) List of one or more Uniform Resource Locators (URLs) that point to API sub-resources, related API resources, or both. RFC 5988 outlines these relationships. (see [below for nested schema](#nestedatt--type_kafka--security--links))

<a id="nestedatt--type_kafka--security--links"></a>
### Nested Schema for `type_kafka.security.links`

Read-Only:

- `href` (String) Uniform Resource Locator (URL) that points another API resource to which this response has some relationship. This URL often begins with `https://cloud.mongodb.com/api/atlas`.
- `rel` (String) Uniform Resource Locator (URL) that defines the semantic relationship between this resource and another API resource. This URL often begins with `https://cloud.mongodb.com/api/atlas`.




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

Optional:

- `aws` (Attributes) AWS configurations for AWS-based connection types. (see [below for nested schema](#nestedatt--type_s3--aws))

<a id="nestedatt--type_s3--aws"></a>
### Nested Schema for `type_s3.aws`

Optional:

- `role_arn` (String) Amazon Resource Name (ARN) that identifies the Amazon Web Services (AWS) Identity and Access Management (IAM) role that MongoDB Cloud assumes when it accesses resources in your AWS account.
- `test_bucket` (String) The name of an S3 bucket used to check authorization of the passed-in IAM role ARN.

Read-Only:

- `links` (Attributes List) List of one or more Uniform Resource Locators (URLs) that point to API sub-resources, related API resources, or both. RFC 5988 outlines these relationships. (see [below for nested schema](#nestedatt--type_s3--aws--links))

<a id="nestedatt--type_s3--aws--links"></a>
### Nested Schema for `type_s3.aws.links`

Read-Only:

- `href` (String) Uniform Resource Locator (URL) that points another API resource to which this response has some relationship. This URL often begins with `https://cloud.mongodb.com/api/atlas`.
- `rel` (String) Uniform Resource Locator (URL) that defines the semantic relationship between this resource and another API resource. This URL often begins with `https://cloud.mongodb.com/api/atlas`.




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

Optional:

- `type` (String) This should always be set to Sample, but can also be omitted


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

Read-Only:

- `href` (String) Uniform Resource Locator (URL) that points another API resource to which this response has some relationship. This URL often begins with `https://cloud.mongodb.com/api/atlas`.
- `rel` (String) Uniform Resource Locator (URL) that defines the semantic relationship between this resource and another API resource. This URL often begins with `https://cloud.mongodb.com/api/atlas`.
48 changes: 48 additions & 0 deletions internal/common/autogen/marshal.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ package autogen
import (
"encoding/json"
"fmt"
"maps"
"reflect"
"strings"

"github.com/hashicorp/terraform-plugin-framework/attr"
"github.com/hashicorp/terraform-plugin-framework/types"
Expand All @@ -16,6 +18,28 @@ const (
tagValOmitJSONUpdate = "omitjsonupdate"
)

type DiscriminatorTag struct {
DiscriminatorPropName string
DiscriminatorPropValue string
}

// / IsDiscriminatorTag decodes a autogen:"discriminator:type=Cluster" tag to a DiscriminatorTag struct.
func IsDiscriminatorTag(tag string) *DiscriminatorTag {
if tag == "" || !strings.HasPrefix(tag, "discriminator:") {
return nil
}
// decode the tag
keyValue := strings.TrimPrefix(tag, "discriminator:")
propName, propValue, found := strings.Cut(keyValue, "=")
if !found {
return nil // not a valid discriminator tag
}
return &DiscriminatorTag{
DiscriminatorPropName: propName,
DiscriminatorPropValue: propValue,
}
}

// Marshal gets a Terraform model and marshals it into JSON (e.g. for an Atlas request).
// It supports the following Terraform model types: String, Bool, Int64, Float64, Object, Map, List, Set.
// Attributes that are null or unknown are not marshaled.
Expand Down Expand Up @@ -53,10 +77,34 @@ func marshalAttrs(valModel reflect.Value, isUpdate bool) (map[string]any, error)
if err := marshalAttr(attrNameModel, attrValModel, objJSON); err != nil {
return nil, err
}
if err := handleDiscriminator(tag, attrNameModel, objJSON); err != nil {
return nil, err
}
}
return objJSON, nil
}

func handleDiscriminator(tag, attrNameModel string, objJSON map[string]any) error {
discriminatorTag := IsDiscriminatorTag(tag)
if discriminatorTag == nil {
return nil // not a discriminator tag, nothing to do
}
attrNameJSON := xstrings.ToCamelCase(attrNameModel)
attrValJSON, ok := objJSON[attrNameJSON]
if !ok {
return nil // attribute not found in the JSON, nothing to do (probably Null or Unknown value)
}
// if the discriminator is set, we remove the attribute from the JSON
delete(objJSON, attrNameJSON)
objJSON[discriminatorTag.DiscriminatorPropName] = discriminatorTag.DiscriminatorPropValue // set the discriminator property in the JSON
attrValJSONObject, ok := attrValJSON.(map[string]any)
if !ok {
return fmt.Errorf("discriminator attribute %s must be an object", attrNameJSON)
}
maps.Copy(objJSON, attrValJSONObject) // copy the object attributes to the root JSON
return nil
}

func marshalAttr(attrNameModel string, attrValModel reflect.Value, objJSON map[string]any) error {
attrNameJSON := xstrings.ToCamelCase(attrNameModel)
obj, ok := attrValModel.Interface().(attr.Value)
Expand Down
54 changes: 54 additions & 0 deletions internal/common/autogen/marshal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -285,3 +285,57 @@ func TestMarshalPanic(t *testing.T) {
})
}
}

func TestIsDiscriminatorTag(t *testing.T) {
testCases := map[string]struct {
expected *autogen.DiscriminatorTag
tagValue string
}{
"empty tag": {
tagValue: "",
expected: nil,
},
"valid tag with name": {
tagValue: "discriminator:type=Cluster",
expected: &autogen.DiscriminatorTag{
DiscriminatorPropName: "type",
DiscriminatorPropValue: "Cluster",
},
},
}
for name, tc := range testCases {
t.Run(name, func(t *testing.T) {
tag := autogen.IsDiscriminatorTag(tc.tagValue)
if tc.expected == nil {
assert.Nil(t, tag)
} else {
require.NotNil(t, tag)
assert.Equal(t, tc.expected.DiscriminatorPropName, tag.DiscriminatorPropName)
assert.Equal(t, tc.expected.DiscriminatorPropValue, tag.DiscriminatorPropValue)
}
})
}
}

func TestMarshalDiscriminator(t *testing.T) {
testCases := map[string]struct {
model streamConn
expectedJSON string
}{
"cluster model": {
model: streamConnModelCluster,
expectedJSON: jsonRespCluster,
},
"https model": {
model: streamConnModelHTTPS,
expectedJSON: jsonRespHTTPS,
},
}
for name, tc := range testCases {
t.Run(name, func(t *testing.T) {
raw, err := autogen.Marshal(&tc.model, false)
require.NoError(t, err)
assert.JSONEq(t, tc.expectedJSON, string(raw))
})
}
}
Loading
Loading