-
Notifications
You must be signed in to change notification settings - Fork 4.6k
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_stream_analytics_output_table
#13854
Merged
Merged
Changes from 6 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
5702d81
initial draft with typed resource
stephybun 181bbd9
add test and registration
stephybun db7fb18
register typed resource and add docs
stephybun fe28f6a
fmt and lint
stephybun 73ebbb1
terrafmt docs
stephybun 7a6d7bd
register typed resource in alphabetical order
stephybun 3f51dde
consolidate if conditions
stephybun File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
287 changes: 287 additions & 0 deletions
287
internal/services/streamanalytics/stream_analytics_output_table_resource.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||||
---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,287 @@ | ||||||||
package streamanalytics | ||||||||
|
||||||||
import ( | ||||||||
"context" | ||||||||
"fmt" | ||||||||
"time" | ||||||||
|
||||||||
"github.com/Azure/azure-sdk-for-go/services/preview/streamanalytics/mgmt/2020-03-01-preview/streamanalytics" | ||||||||
"github.com/hashicorp/go-azure-helpers/response" | ||||||||
"github.com/hashicorp/terraform-provider-azurerm/helpers/azure" | ||||||||
"github.com/hashicorp/terraform-provider-azurerm/internal/sdk" | ||||||||
"github.com/hashicorp/terraform-provider-azurerm/internal/services/streamanalytics/parse" | ||||||||
"github.com/hashicorp/terraform-provider-azurerm/internal/services/streamanalytics/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" | ||||||||
) | ||||||||
|
||||||||
type OutputTableResource struct { | ||||||||
} | ||||||||
|
||||||||
var _ sdk.ResourceWithCustomImporter = OutputTableResource{} | ||||||||
|
||||||||
type OutputTableResourceModel struct { | ||||||||
Name string `tfschema:"name"` | ||||||||
StreamAnalyticsJob string `tfschema:"stream_analytics_job_name"` | ||||||||
ResourceGroup string `tfschema:"resource_group_name"` | ||||||||
StorageAccount string `tfschema:"storage_account_name"` | ||||||||
StorageAccountKey string `tfschema:"storage_account_key"` | ||||||||
Table string `tfschema:"table"` | ||||||||
PartitionKey string `tfschema:"partition_key"` | ||||||||
RowKey string `tfschema:"row_key"` | ||||||||
BatchSize int32 `tfschema:"batch_size"` | ||||||||
} | ||||||||
|
||||||||
func (r OutputTableResource) Arguments() map[string]*pluginsdk.Schema { | ||||||||
return map[string]*pluginsdk.Schema{ | ||||||||
"name": { | ||||||||
Type: pluginsdk.TypeString, | ||||||||
Required: true, | ||||||||
ForceNew: true, | ||||||||
ValidateFunc: validation.StringIsNotEmpty, | ||||||||
}, | ||||||||
|
||||||||
"stream_analytics_job_name": { | ||||||||
Type: pluginsdk.TypeString, | ||||||||
Required: true, | ||||||||
ForceNew: true, | ||||||||
ValidateFunc: validation.StringIsNotEmpty, | ||||||||
}, | ||||||||
|
||||||||
"resource_group_name": azure.SchemaResourceGroupName(), | ||||||||
|
||||||||
"storage_account_name": { | ||||||||
Type: pluginsdk.TypeString, | ||||||||
Required: true, | ||||||||
ValidateFunc: validation.StringIsNotEmpty, | ||||||||
}, | ||||||||
|
||||||||
"storage_account_key": { | ||||||||
Type: pluginsdk.TypeString, | ||||||||
Required: true, | ||||||||
Sensitive: true, | ||||||||
ValidateFunc: validation.StringIsNotEmpty, | ||||||||
}, | ||||||||
|
||||||||
"table": { | ||||||||
Type: pluginsdk.TypeString, | ||||||||
Required: true, | ||||||||
ValidateFunc: validation.StringIsNotEmpty, | ||||||||
}, | ||||||||
|
||||||||
"partition_key": { | ||||||||
Type: pluginsdk.TypeString, | ||||||||
Required: true, | ||||||||
ValidateFunc: validation.StringIsNotEmpty, | ||||||||
}, | ||||||||
|
||||||||
"row_key": { | ||||||||
Type: pluginsdk.TypeString, | ||||||||
Required: true, | ||||||||
ValidateFunc: validation.StringIsNotEmpty, | ||||||||
}, | ||||||||
|
||||||||
"batch_size": { | ||||||||
Type: pluginsdk.TypeInt, | ||||||||
Required: true, | ||||||||
ValidateFunc: validation.IntBetween(1, 100), | ||||||||
}, | ||||||||
} | ||||||||
} | ||||||||
|
||||||||
func (r OutputTableResource) Attributes() map[string]*pluginsdk.Schema { | ||||||||
return map[string]*pluginsdk.Schema{} | ||||||||
} | ||||||||
|
||||||||
func (r OutputTableResource) ModelObject() interface{} { | ||||||||
return &OutputTableResourceModel{} | ||||||||
} | ||||||||
|
||||||||
func (r OutputTableResource) ResourceType() string { | ||||||||
return "azurerm_stream_analytics_output_table" | ||||||||
} | ||||||||
|
||||||||
func (r OutputTableResource) IDValidationFunc() pluginsdk.SchemaValidateFunc { | ||||||||
return validate.OutputID | ||||||||
} | ||||||||
|
||||||||
func (r OutputTableResource) Create() sdk.ResourceFunc { | ||||||||
return sdk.ResourceFunc{ | ||||||||
Timeout: 30 * time.Minute, | ||||||||
Func: func(ctx context.Context, metadata sdk.ResourceMetaData) error { | ||||||||
var model OutputTableResourceModel | ||||||||
if err := metadata.Decode(&model); err != nil { | ||||||||
return err | ||||||||
} | ||||||||
|
||||||||
client := metadata.Client.StreamAnalytics.OutputsClient | ||||||||
subscriptionId := metadata.Client.Account.SubscriptionId | ||||||||
|
||||||||
id := parse.NewOutputID(subscriptionId, model.ResourceGroup, model.StreamAnalyticsJob, model.Name) | ||||||||
|
||||||||
existing, err := client.Get(ctx, id.ResourceGroup, id.StreamingjobName, 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) | ||||||||
} | ||||||||
|
||||||||
tableOutputProps := &streamanalytics.AzureTableOutputDataSourceProperties{ | ||||||||
AccountName: utils.String(model.StorageAccount), | ||||||||
AccountKey: utils.String(model.StorageAccountKey), | ||||||||
Table: utils.String(model.Table), | ||||||||
PartitionKey: utils.String(model.PartitionKey), | ||||||||
RowKey: utils.String(model.RowKey), | ||||||||
BatchSize: utils.Int32(model.BatchSize), | ||||||||
} | ||||||||
|
||||||||
props := streamanalytics.Output{ | ||||||||
Name: utils.String(model.Name), | ||||||||
OutputProperties: &streamanalytics.OutputProperties{ | ||||||||
Datasource: &streamanalytics.AzureTableOutputDataSource{ | ||||||||
Type: streamanalytics.TypeMicrosoftStorageTable, | ||||||||
AzureTableOutputDataSourceProperties: tableOutputProps, | ||||||||
}, | ||||||||
}, | ||||||||
} | ||||||||
|
||||||||
_, err = client.CreateOrReplace(ctx, props, id.ResourceGroup, id.StreamingjobName, id.Name, "", "") | ||||||||
if err != nil { | ||||||||
return fmt.Errorf("creating %s: %+v", id, err) | ||||||||
} | ||||||||
|
||||||||
metadata.SetID(id) | ||||||||
|
||||||||
return nil | ||||||||
}, | ||||||||
} | ||||||||
} | ||||||||
|
||||||||
func (r OutputTableResource) Read() sdk.ResourceFunc { | ||||||||
return sdk.ResourceFunc{ | ||||||||
Timeout: 5 * time.Minute, | ||||||||
Func: func(ctx context.Context, metadata sdk.ResourceMetaData) error { | ||||||||
client := metadata.Client.StreamAnalytics.OutputsClient | ||||||||
id, err := parse.OutputID(metadata.ResourceData.Id()) | ||||||||
if err != nil { | ||||||||
return err | ||||||||
} | ||||||||
|
||||||||
resp, err := client.Get(ctx, id.ResourceGroup, id.StreamingjobName, id.Name) | ||||||||
if err != nil { | ||||||||
if utils.ResponseWasNotFound(resp.Response) { | ||||||||
return metadata.MarkAsGone(id) | ||||||||
} | ||||||||
return fmt.Errorf("reading %s: %+v", *id, err) | ||||||||
} | ||||||||
|
||||||||
if props := resp.OutputProperties; props != nil { | ||||||||
v, ok := props.Datasource.AsAzureTableOutputDataSource() | ||||||||
if !ok { | ||||||||
return fmt.Errorf("converting output data source to a blob output: %+v", err) | ||||||||
} | ||||||||
|
||||||||
state := OutputTableResourceModel{ | ||||||||
Name: id.Name, | ||||||||
ResourceGroup: id.ResourceGroup, | ||||||||
StreamAnalyticsJob: id.StreamingjobName, | ||||||||
StorageAccount: *v.AccountName, | ||||||||
StorageAccountKey: metadata.ResourceData.Get("storage_account_key").(string), | ||||||||
Table: *v.Table, | ||||||||
PartitionKey: *v.PartitionKey, | ||||||||
RowKey: *v.RowKey, | ||||||||
BatchSize: *v.BatchSize, | ||||||||
} | ||||||||
return metadata.Encode(&state) | ||||||||
} | ||||||||
return nil | ||||||||
}, | ||||||||
} | ||||||||
} | ||||||||
|
||||||||
func (r OutputTableResource) Update() sdk.ResourceFunc { | ||||||||
return sdk.ResourceFunc{ | ||||||||
Timeout: 30 * time.Minute, | ||||||||
Func: func(ctx context.Context, metadata sdk.ResourceMetaData) error { | ||||||||
client := metadata.Client.StreamAnalytics.OutputsClient | ||||||||
id, err := parse.OutputID(metadata.ResourceData.Id()) | ||||||||
if err != nil { | ||||||||
return err | ||||||||
} | ||||||||
|
||||||||
var state OutputTableResourceModel | ||||||||
if err := metadata.Decode(&state); err != nil { | ||||||||
return fmt.Errorf("decoding: %+v", err) | ||||||||
} | ||||||||
|
||||||||
props := streamanalytics.Output{ | ||||||||
Name: utils.String(state.Name), | ||||||||
OutputProperties: &streamanalytics.OutputProperties{ | ||||||||
Datasource: &streamanalytics.AzureTableOutputDataSource{ | ||||||||
Type: streamanalytics.TypeMicrosoftStorageTable, | ||||||||
AzureTableOutputDataSourceProperties: &streamanalytics.AzureTableOutputDataSourceProperties{ | ||||||||
AccountName: utils.String(state.StorageAccount), | ||||||||
AccountKey: utils.String(state.StorageAccountKey), | ||||||||
Table: utils.String(state.Table), | ||||||||
PartitionKey: utils.String(state.PartitionKey), | ||||||||
RowKey: utils.String(state.RowKey), | ||||||||
BatchSize: utils.Int32(state.BatchSize), | ||||||||
}, | ||||||||
}, | ||||||||
}, | ||||||||
} | ||||||||
|
||||||||
_, err = client.Update(ctx, props, id.ResourceGroup, id.StreamingjobName, id.Name, "") | ||||||||
if err != nil { | ||||||||
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. and here
Suggested change
|
||||||||
return fmt.Errorf("updating %s: %+v", *id, err) | ||||||||
} | ||||||||
return nil | ||||||||
}, | ||||||||
} | ||||||||
} | ||||||||
|
||||||||
func (r OutputTableResource) Delete() sdk.ResourceFunc { | ||||||||
return sdk.ResourceFunc{ | ||||||||
Timeout: 30 * time.Minute, | ||||||||
Func: func(ctx context.Context, metadata sdk.ResourceMetaData) error { | ||||||||
client := metadata.Client.StreamAnalytics.OutputsClient | ||||||||
id, err := parse.OutputID(metadata.ResourceData.Id()) | ||||||||
if err != nil { | ||||||||
return err | ||||||||
} | ||||||||
|
||||||||
metadata.Logger.Infof("deleting %s", *id) | ||||||||
|
||||||||
if resp, err := client.Delete(ctx, id.ResourceGroup, id.StreamingjobName, id.Name); err != nil { | ||||||||
if !response.WasNotFound(resp.Response) { | ||||||||
return fmt.Errorf("deleting %s: %+v", *id, err) | ||||||||
} | ||||||||
} | ||||||||
return nil | ||||||||
}, | ||||||||
} | ||||||||
} | ||||||||
|
||||||||
func (r OutputTableResource) CustomImporter() sdk.ResourceRunFunc { | ||||||||
return func(ctx context.Context, metadata sdk.ResourceMetaData) error { | ||||||||
id, err := parse.OutputID(metadata.ResourceData.Id()) | ||||||||
if err != nil { | ||||||||
return err | ||||||||
} | ||||||||
|
||||||||
client := metadata.Client.StreamAnalytics.OutputsClient | ||||||||
resp, err := client.Get(ctx, id.ResourceGroup, id.StreamingjobName, id.Name) | ||||||||
if err != nil || resp.OutputProperties == nil { | ||||||||
return fmt.Errorf("reading %s: %+v", *id, err) | ||||||||
} | ||||||||
|
||||||||
props := resp.OutputProperties | ||||||||
if _, ok := props.Datasource.AsAzureTableOutputDataSource(); !ok { | ||||||||
return fmt.Errorf("specified output is not of type %s", streamanalytics.TypeMicrosoftStorageTable) | ||||||||
} | ||||||||
return nil | ||||||||
} | ||||||||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
these lines could be combined