-
Notifications
You must be signed in to change notification settings - Fork 4.7k
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
New Resource: azurerm_data_factory_dataset_cosmosdb_mongoapi #28185
base: main
Are you sure you want to change the base?
Changes from 3 commits
7358021
1179a42
f65b76f
d95a18f
d19f37e
645977d
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 |
---|---|---|
@@ -0,0 +1,339 @@ | ||
package datafactory | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"time" | ||
|
||
"github.com/hashicorp/go-azure-helpers/lang/pointer" | ||
"github.com/hashicorp/go-azure-sdk/resource-manager/datafactory/2018-06-01/factories" | ||
"github.com/hashicorp/terraform-provider-azurerm/internal/sdk" | ||
"github.com/hashicorp/terraform-provider-azurerm/internal/services/datafactory/parse" | ||
"github.com/hashicorp/terraform-provider-azurerm/internal/services/datafactory/validate" | ||
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/pluginsdk" | ||
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/validation" | ||
"github.com/hashicorp/terraform-provider-azurerm/utils" | ||
"github.com/jackofallops/kermit/sdk/datafactory/2018-06-01/datafactory" | ||
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. Is there a reason we're using 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. Have switched to go-azure-sdk in d95a18f. There are tons of other resources in this folder still using kermit though, I've decided they're out of scope of this PR. |
||
) | ||
|
||
var _ sdk.Resource = DataFactoryDatasetCosmosDbMongoDbResource{} | ||
|
||
type DataFactoryDatasetCosmosDbMongoDbResource struct{} | ||
|
||
type DataFactoryDatasetCosmosDbMongoDbResourceModel struct { | ||
AdditionalProperties map[string]string `tfschema:"additional_properties"` | ||
Annotations []string `tfschema:"annotations"` | ||
CollectionName string `tfschema:"collection_name"` | ||
DataFactoryId string `tfschema:"data_factory_id"` | ||
Description string `tfschema:"description"` | ||
Folder string `tfschema:"folder"` | ||
LinkedServiceName string `tfschema:"linked_service_name"` | ||
Name string `tfschema:"name"` | ||
Parameters map[string]string `tfschema:"parameters"` | ||
} | ||
|
||
func (DataFactoryDatasetCosmosDbMongoDbResource) Arguments() map[string]*pluginsdk.Schema { | ||
return map[string]*pluginsdk.Schema{ | ||
"name": { | ||
Type: pluginsdk.TypeString, | ||
Required: true, | ||
ForceNew: true, | ||
ValidateFunc: validate.LinkedServiceDatasetName, | ||
}, | ||
"data_factory_id": { | ||
Type: pluginsdk.TypeString, | ||
Required: true, | ||
ForceNew: true, | ||
ValidateFunc: factories.ValidateFactoryID, | ||
}, | ||
"linked_service_name": { | ||
Type: pluginsdk.TypeString, | ||
Required: true, | ||
ValidateFunc: validation.StringIsNotEmpty, | ||
}, | ||
"additional_properties": { | ||
Type: pluginsdk.TypeMap, | ||
Optional: true, | ||
Elem: &pluginsdk.Schema{ | ||
Type: pluginsdk.TypeString, | ||
}, | ||
}, | ||
"annotations": { | ||
Type: pluginsdk.TypeList, | ||
Optional: true, | ||
Elem: &pluginsdk.Schema{ | ||
Type: pluginsdk.TypeString, | ||
}, | ||
}, | ||
"collection_name": { | ||
Type: pluginsdk.TypeString, | ||
Required: true, | ||
ValidateFunc: validation.StringIsNotEmpty, | ||
}, | ||
"description": { | ||
Type: pluginsdk.TypeString, | ||
Optional: true, | ||
ValidateFunc: validation.StringIsNotEmpty, | ||
}, | ||
"folder": { | ||
Type: pluginsdk.TypeString, | ||
Optional: true, | ||
ValidateFunc: validation.StringIsNotEmpty, | ||
}, | ||
"parameters": { | ||
Type: pluginsdk.TypeMap, | ||
Optional: true, | ||
Elem: &pluginsdk.Schema{ | ||
Type: pluginsdk.TypeString, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func (DataFactoryDatasetCosmosDbMongoDbResource) Attributes() map[string]*pluginsdk.Schema { | ||
return map[string]*pluginsdk.Schema{} | ||
} | ||
|
||
func (DataFactoryDatasetCosmosDbMongoDbResource) ModelObject() interface{} { | ||
return &DataFactoryDatasetCosmosDbMongoDbResourceModel{} | ||
} | ||
|
||
func (DataFactoryDatasetCosmosDbMongoDbResource) ResourceType() string { | ||
return "azurerm_data_factory_dataset_cosmosdb_mongoapi" | ||
} | ||
|
||
func (r DataFactoryDatasetCosmosDbMongoDbResource) Create() sdk.ResourceFunc { | ||
return sdk.ResourceFunc{ | ||
Timeout: 30 * time.Minute, | ||
|
||
Func: func(ctx context.Context, metadata sdk.ResourceMetaData) error { | ||
client := metadata.Client.DataFactory.DatasetClient | ||
subscriptionId := metadata.Client.DataFactory.DatasetClient.SubscriptionID | ||
|
||
var config DataFactoryDatasetCosmosDbMongoDbResourceModel | ||
if err := metadata.Decode(&config); err != nil { | ||
return fmt.Errorf("decoding: %+v", err) | ||
} | ||
|
||
dataFactoryId, err := factories.ParseFactoryID(config.DataFactoryId) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
id := parse.NewDataSetID(subscriptionId, dataFactoryId.ResourceGroupName, dataFactoryId.FactoryName, config.Name) | ||
|
||
existing, err := client.Get(ctx, id.ResourceGroup, id.FactoryName, id.Name, "") | ||
if err != nil && !utils.ResponseWasNotFound(existing.Response) { | ||
return fmt.Errorf("checking for presence of existing %s: %+v", id, err) | ||
} | ||
if !utils.ResponseWasNotFound(existing.Response) { | ||
return metadata.ResourceRequiresImport(r.ResourceType(), id) | ||
} | ||
|
||
datasetProperties := datafactory.CosmosDbMongoDbAPICollectionDataset{ | ||
CosmosDbMongoDbAPICollectionDatasetTypeProperties: &datafactory.CosmosDbMongoDbAPICollectionDatasetTypeProperties{ | ||
Collection: config.CollectionName, | ||
}, | ||
LinkedServiceName: &datafactory.LinkedServiceReference{ | ||
ReferenceName: &config.LinkedServiceName, | ||
Type: pointer.To("LinkedServiceReference"), | ||
}, | ||
} | ||
|
||
if config.AdditionalProperties != nil { | ||
datasetProperties.AdditionalProperties = expandAdditionalProperties(&config.AdditionalProperties) | ||
} | ||
|
||
if config.Annotations != nil { | ||
datasetProperties.Annotations = pointer.To(utils.FlattenStringSlice(&config.Annotations)) | ||
} | ||
|
||
if config.Description != "" { | ||
datasetProperties.Description = &config.Description | ||
} | ||
|
||
if config.Folder != "" { | ||
datasetProperties.Folder = &datafactory.DatasetFolder{ | ||
Name: &config.Folder, | ||
} | ||
} | ||
|
||
if config.Parameters != nil { | ||
datasetProperties.Parameters = expandDataSetParametersString(&config.Parameters) | ||
} | ||
|
||
dataset := datafactory.DatasetResource{ | ||
Properties: &datasetProperties, | ||
Type: pointer.To(string(datafactory.TypeBasicDatasetTypeCosmosDbMongoDbAPICollection)), | ||
} | ||
|
||
if _, err := client.CreateOrUpdate(ctx, id.ResourceGroup, id.FactoryName, id.Name, dataset, ""); err != nil { | ||
return fmt.Errorf("creating/updating %s: %+v", id, err) | ||
} | ||
|
||
metadata.SetID(id) | ||
return nil | ||
}, | ||
} | ||
} | ||
|
||
func (r DataFactoryDatasetCosmosDbMongoDbResource) Update() sdk.ResourceFunc { | ||
return sdk.ResourceFunc{ | ||
Timeout: 30 * time.Minute, | ||
|
||
Func: func(ctx context.Context, metadata sdk.ResourceMetaData) error { | ||
client := metadata.Client.DataFactory.DatasetClient | ||
|
||
var config DataFactoryDatasetCosmosDbMongoDbResourceModel | ||
if err := metadata.Decode(&config); err != nil { | ||
return fmt.Errorf("decoding: %+v", err) | ||
} | ||
|
||
id, err := parse.DataSetID(metadata.ResourceData.Id()) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
dataset, err := client.Get(ctx, id.ResourceGroup, id.FactoryName, id.Name, "") | ||
if err != nil { | ||
return fmt.Errorf("checking for presence of existing %s: %+v", id, err) | ||
} | ||
|
||
datasetProperties, ok := dataset.Properties.AsCosmosDbMongoDbAPICollectionDataset() | ||
if !ok { | ||
return fmt.Errorf("dataset %s is not a CosmosDbMongoDbAPICollectionDataset", id) | ||
} | ||
|
||
if metadata.ResourceData.HasChange("additional_properties") { | ||
datasetProperties.AdditionalProperties = expandAdditionalProperties(&config.AdditionalProperties) | ||
} | ||
|
||
if metadata.ResourceData.HasChange("annotations") { | ||
datasetProperties.Annotations = pointer.To(utils.FlattenStringSlice(&config.Annotations)) | ||
} | ||
|
||
if metadata.ResourceData.HasChange("collection_name") { | ||
datasetProperties.CosmosDbMongoDbAPICollectionDatasetTypeProperties.Collection = config.CollectionName | ||
} | ||
|
||
if metadata.ResourceData.HasChange("description") { | ||
datasetProperties.Description = pointer.To(config.Description) | ||
} | ||
|
||
if metadata.ResourceData.HasChange("folder") { | ||
datasetProperties.Folder = &datafactory.DatasetFolder{ | ||
Name: &config.Folder, | ||
} | ||
} | ||
|
||
if metadata.ResourceData.HasChange("linked_service_name") { | ||
datasetProperties.LinkedServiceName = &datafactory.LinkedServiceReference{ | ||
ReferenceName: &config.LinkedServiceName, | ||
Type: pointer.To("LinkedServiceReference"), | ||
} | ||
} | ||
|
||
if metadata.ResourceData.HasChange("parameters") { | ||
datasetProperties.Parameters = expandDataSetParametersString(&config.Parameters) | ||
} | ||
|
||
dataset.Properties = datasetProperties | ||
|
||
if _, err := client.CreateOrUpdate(ctx, id.ResourceGroup, id.FactoryName, id.Name, dataset, ""); err != nil { | ||
return fmt.Errorf("updating %s: %+v", id, err) | ||
} | ||
|
||
return nil | ||
}, | ||
} | ||
} | ||
|
||
func (DataFactoryDatasetCosmosDbMongoDbResource) Read() sdk.ResourceFunc { | ||
return sdk.ResourceFunc{ | ||
Timeout: 5 * time.Minute, | ||
|
||
Func: func(ctx context.Context, metadata sdk.ResourceMetaData) error { | ||
client := metadata.Client.DataFactory.DatasetClient | ||
|
||
id, err := parse.DataSetID(metadata.ResourceData.Id()) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
dataset, err := client.Get(ctx, id.ResourceGroup, id.FactoryName, id.Name, "") | ||
if err != nil { | ||
if utils.ResponseWasNotFound(dataset.Response) { | ||
return metadata.MarkAsGone(id) | ||
} | ||
|
||
return fmt.Errorf("retrieving %s: %+v", id, err) | ||
} | ||
|
||
datasetProperties, ok := dataset.Properties.AsCosmosDbMongoDbAPICollectionDataset() | ||
if !ok { | ||
return fmt.Errorf("dataset %s is not a CosmosDbMongoDbAPICollectionDataset", id) | ||
} | ||
|
||
state := DataFactoryDatasetCosmosDbMongoDbResourceModel{} | ||
|
||
state.AdditionalProperties = flattenAdditionalProperties(&datasetProperties.AdditionalProperties) | ||
|
||
if datasetProperties.Annotations != nil { | ||
state.Annotations = flattenDataFactoryAnnotations(datasetProperties.Annotations) | ||
} | ||
|
||
if collectionName, ok := datasetProperties.Collection.(string); ok { | ||
state.CollectionName = collectionName | ||
} | ||
|
||
state.DataFactoryId = factories.NewFactoryID(id.SubscriptionId, id.ResourceGroup, id.FactoryName).ID() | ||
|
||
if datasetProperties.Description != nil { | ||
state.Description = *datasetProperties.Description | ||
} | ||
|
||
if datasetProperties.Folder != nil { | ||
if datasetProperties.Folder.Name != nil { | ||
state.Folder = *datasetProperties.Folder.Name | ||
} | ||
} | ||
|
||
if datasetProperties.LinkedServiceName != nil { | ||
if datasetProperties.LinkedServiceName.ReferenceName != nil { | ||
state.LinkedServiceName = *datasetProperties.LinkedServiceName.ReferenceName | ||
} | ||
} | ||
|
||
state.Name = id.Name | ||
|
||
state.Parameters = flattenDataSetParametersString(&datasetProperties.Parameters) | ||
|
||
return metadata.Encode(&state) | ||
}, | ||
} | ||
} | ||
|
||
func (DataFactoryDatasetCosmosDbMongoDbResource) Delete() sdk.ResourceFunc { | ||
return sdk.ResourceFunc{ | ||
Timeout: 30 * time.Minute, | ||
|
||
Func: func(ctx context.Context, metadata sdk.ResourceMetaData) error { | ||
client := metadata.Client.DataFactory.DatasetClient | ||
|
||
id, err := parse.DataSetID(metadata.ResourceData.Id()) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if _, err := client.Delete(ctx, id.ResourceGroup, id.FactoryName, id.Name); err != nil { | ||
return fmt.Errorf("deleting %s: %+v", id, err) | ||
} | ||
|
||
return nil | ||
}, | ||
} | ||
} | ||
|
||
func (DataFactoryDatasetCosmosDbMongoDbResource) IDValidationFunc() pluginsdk.SchemaValidateFunc { | ||
return validate.LinkedServiceDatasetName | ||
} |
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.
This filename seems so be mismatched from the resource name? Should it be
data_factory_dataset_cosmosdb_mongodb_resource.go
, or should the resource model be namedDataFactoryDatasetCosmosDbMongoAPIResource
instead?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.
Good catch, have fixed this into
_mondodbapi_resource
and corresponding go model type.d95a18f