Skip to content
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_job_schedule #16349

Merged
merged 3 commits into from
Apr 14, 2022
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package parse

// NOTE: this file is generated via 'go:generate' - manual changes will be overwritten

import (
"fmt"
"strings"

"github.com/hashicorp/go-azure-helpers/resourcemanager/resourceids"
)

type StreamingJobScheduleId struct {
SubscriptionId string
ResourceGroup string
StreamingjobName string
ScheduleName string
}

func NewStreamingJobScheduleID(subscriptionId, resourceGroup, streamingjobName, scheduleName string) StreamingJobScheduleId {
return StreamingJobScheduleId{
SubscriptionId: subscriptionId,
ResourceGroup: resourceGroup,
StreamingjobName: streamingjobName,
ScheduleName: scheduleName,
}
}

func (id StreamingJobScheduleId) String() string {
segments := []string{
fmt.Sprintf("Schedule Name %q", id.ScheduleName),
fmt.Sprintf("Streamingjob Name %q", id.StreamingjobName),
fmt.Sprintf("Resource Group %q", id.ResourceGroup),
}
segmentsStr := strings.Join(segments, " / ")
return fmt.Sprintf("%s: (%s)", "Streaming Job Schedule", segmentsStr)
}

func (id StreamingJobScheduleId) ID() string {
fmtString := "/subscriptions/%s/resourceGroups/%s/providers/Microsoft.StreamAnalytics/streamingjobs/%s/schedule/%s"
return fmt.Sprintf(fmtString, id.SubscriptionId, id.ResourceGroup, id.StreamingjobName, id.ScheduleName)
}

// StreamingJobScheduleID parses a StreamingJobSchedule ID into an StreamingJobScheduleId struct
func StreamingJobScheduleID(input string) (*StreamingJobScheduleId, error) {
id, err := resourceids.ParseAzureResourceID(input)
if err != nil {
return nil, err
}

resourceId := StreamingJobScheduleId{
SubscriptionId: id.SubscriptionID,
ResourceGroup: id.ResourceGroup,
}

if resourceId.SubscriptionId == "" {
return nil, fmt.Errorf("ID was missing the 'subscriptions' element")
}

if resourceId.ResourceGroup == "" {
return nil, fmt.Errorf("ID was missing the 'resourceGroups' element")
}

if resourceId.StreamingjobName, err = id.PopSegment("streamingjobs"); err != nil {
return nil, err
}
if resourceId.ScheduleName, err = id.PopSegment("schedule"); err != nil {
return nil, err
}

if err := id.ValidateNoEmptySegments(input); err != nil {
return nil, err
}

return &resourceId, nil
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
package parse

// NOTE: this file is generated via 'go:generate' - manual changes will be overwritten

import (
"testing"

"github.com/hashicorp/go-azure-helpers/resourcemanager/resourceids"
)

var _ resourceids.Id = StreamingJobScheduleId{}

func TestStreamingJobScheduleIDFormatter(t *testing.T) {
actual := NewStreamingJobScheduleID("12345678-1234-9876-4563-123456789012", "resGroup1", "streamingJob1", "default").ID()
expected := "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/resGroup1/providers/Microsoft.StreamAnalytics/streamingjobs/streamingJob1/schedule/default"
if actual != expected {
t.Fatalf("Expected %q but got %q", expected, actual)
}
}

func TestStreamingJobScheduleID(t *testing.T) {
testData := []struct {
Input string
Error bool
Expected *StreamingJobScheduleId
}{

{
// empty
Input: "",
Error: true,
},

{
// missing SubscriptionId
Input: "/",
Error: true,
},

{
// missing value for SubscriptionId
Input: "/subscriptions/",
Error: true,
},

{
// missing ResourceGroup
Input: "/subscriptions/12345678-1234-9876-4563-123456789012/",
Error: true,
},

{
// missing value for ResourceGroup
Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/",
Error: true,
},

{
// missing StreamingjobName
Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/resGroup1/providers/Microsoft.StreamAnalytics/",
Error: true,
},

{
// missing value for StreamingjobName
Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/resGroup1/providers/Microsoft.StreamAnalytics/streamingjobs/",
Error: true,
},

{
// missing ScheduleName
Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/resGroup1/providers/Microsoft.StreamAnalytics/streamingjobs/streamingJob1/",
Error: true,
},

{
// missing value for ScheduleName
Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/resGroup1/providers/Microsoft.StreamAnalytics/streamingjobs/streamingJob1/schedule/",
Error: true,
},

{
// valid
Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/resGroup1/providers/Microsoft.StreamAnalytics/streamingjobs/streamingJob1/schedule/default",
Expected: &StreamingJobScheduleId{
SubscriptionId: "12345678-1234-9876-4563-123456789012",
ResourceGroup: "resGroup1",
StreamingjobName: "streamingJob1",
ScheduleName: "default",
},
},

{
// upper-cased
Input: "/SUBSCRIPTIONS/12345678-1234-9876-4563-123456789012/RESOURCEGROUPS/RESGROUP1/PROVIDERS/MICROSOFT.STREAMANALYTICS/STREAMINGJOBS/STREAMINGJOB1/SCHEDULE/DEFAULT",
Error: true,
},
}

for _, v := range testData {
t.Logf("[DEBUG] Testing %q", v.Input)

actual, err := StreamingJobScheduleID(v.Input)
if err != nil {
if v.Error {
continue
}

t.Fatalf("Expect a value but got an error: %s", err)
}
if v.Error {
t.Fatal("Expect an error but didn't get one")
}

if actual.SubscriptionId != v.Expected.SubscriptionId {
t.Fatalf("Expected %q but got %q for SubscriptionId", v.Expected.SubscriptionId, actual.SubscriptionId)
}
if actual.ResourceGroup != v.Expected.ResourceGroup {
t.Fatalf("Expected %q but got %q for ResourceGroup", v.Expected.ResourceGroup, actual.ResourceGroup)
}
if actual.StreamingjobName != v.Expected.StreamingjobName {
t.Fatalf("Expected %q but got %q for StreamingjobName", v.Expected.StreamingjobName, actual.StreamingjobName)
}
if actual.ScheduleName != v.Expected.ScheduleName {
t.Fatalf("Expected %q but got %q for ScheduleName", v.Expected.ScheduleName, actual.ScheduleName)
}
}
}
5 changes: 3 additions & 2 deletions internal/services/streamanalytics/registration.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,11 @@ func (r Registration) DataSources() []sdk.DataSource {

func (r Registration) Resources() []sdk.Resource {
return []sdk.Resource{
OutputFunctionResource{},
OutputTableResource{},
ClusterResource{},
JobScheduleResource{},
ManagedPrivateEndpointResource{},
OutputFunctionResource{},
OutputTableResource{},
}
}

Expand Down
1 change: 1 addition & 0 deletions internal/services/streamanalytics/resourceids.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package streamanalytics

//go:generate go run ../../tools/generator-resource-id/main.go -path=./ -name=Function -id=/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/resGroup1/providers/Microsoft.StreamAnalytics/streamingjobs/streamingJob1/functions/function1
//go:generate go run ../../tools/generator-resource-id/main.go -path=./ -name=StreamingJob -id=/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/resGroup1/providers/Microsoft.StreamAnalytics/streamingjobs/streamingJob1
//go:generate go run ../../tools/generator-resource-id/main.go -path=./ -name=StreamingJobSchedule -id=/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/resGroup1/providers/Microsoft.StreamAnalytics/streamingjobs/streamingJob1/schedule/default
//go:generate go run ../../tools/generator-resource-id/main.go -path=./ -name=StreamInput -id=/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/resGroup1/providers/Microsoft.StreamAnalytics/streamingjobs/streamingJob1/inputs/streamInput1
//go:generate go run ../../tools/generator-resource-id/main.go -path=./ -name=Output -id=/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/resGroup1/providers/Microsoft.StreamAnalytics/streamingjobs/streamingJob1/outputs/output1
//go:generate go run ../../tools/generator-resource-id/main.go -path=./ -name=Cluster -id=/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/resGroup1/providers/Microsoft.StreamAnalytics/clusters/cluster1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,26 @@ func dataSourceStreamAnalyticsJob() *pluginsdk.Resource {

"identity": commonschema.SystemAssignedIdentityComputed(),

"last_output_time": {
Type: pluginsdk.TypeString,
Computed: true,
},

"output_error_policy": {
Type: pluginsdk.TypeString,
Computed: true,
},

"start_mode": {
Type: pluginsdk.TypeString,
Computed: true,
},

"start_time": {
Type: pluginsdk.TypeString,
Computed: true,
},

"streaming_units": {
Type: pluginsdk.TypeInt,
Computed: true,
Expand Down Expand Up @@ -110,16 +125,26 @@ func dataSourceStreamAnalyticsJobRead(d *pluginsdk.ResourceData, meta interface{
if props := resp.StreamingJobProperties; props != nil {
d.Set("compatibility_level", string(props.CompatibilityLevel))
d.Set("data_locale", props.DataLocale)
if props.EventsLateArrivalMaxDelayInSeconds != nil {
d.Set("events_late_arrival_max_delay_in_seconds", int(*props.EventsLateArrivalMaxDelayInSeconds))
if v := props.EventsLateArrivalMaxDelayInSeconds; v != nil {
d.Set("events_late_arrival_max_delay_in_seconds", int(*v))
}
if props.EventsOutOfOrderMaxDelayInSeconds != nil {
d.Set("events_out_of_order_max_delay_in_seconds", int(*props.EventsOutOfOrderMaxDelayInSeconds))
if v := props.EventsOutOfOrderMaxDelayInSeconds; v != nil {
d.Set("events_out_of_order_max_delay_in_seconds", int(*v))
}
d.Set("events_out_of_order_policy", string(props.EventsOutOfOrderPolicy))
d.Set("job_id", props.JobID)
d.Set("output_error_policy", string(props.OutputErrorPolicy))

if v := props.LastOutputEventTime; v != nil {
d.Set("last_output_time", v.String())
}

if v := props.OutputStartTime; v != nil {
d.Set("start_time", v.String())
}

d.Set("start_mode", props.OutputStartMode)

if props.Transformation != nil && props.Transformation.TransformationProperties != nil {
d.Set("streaming_units", props.Transformation.TransformationProperties.StreamingUnits)
d.Set("transformation_query", props.Transformation.TransformationProperties.Query)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,20 @@ func TestAccDataSourceStreamAnalyticsJob_identity(t *testing.T) {
})
}

func TestAccDataSourceStreamAnalyticsJob_jobSchedule(t *testing.T) {
data := acceptance.BuildTestData(t, "data.azurerm_stream_analytics_job", "test")

data.DataSourceTest(t, []acceptance.TestStep{
{
Config: StreamAnalyticsJobDataSource{}.jobSchedule(data),
Check: acceptance.ComposeTestCheckFunc(
check.That(data.ResourceName).Key("start_mode").Exists(),
check.That(data.ResourceName).Key("start_time").Exists(),
),
},
})
}

func (d StreamAnalyticsJobDataSource) basic(data acceptance.TestData) string {
config := StreamAnalyticsJobResource{}.basic(data)
return fmt.Sprintf(`
Expand All @@ -65,3 +79,17 @@ data "azurerm_stream_analytics_job" "test" {
}
`, config)
}

func (d StreamAnalyticsJobDataSource) jobSchedule(data acceptance.TestData) string {
config := StreamAnalyticsJobScheduleResource{}.customTime(data)
return fmt.Sprintf(`
%s

data "azurerm_stream_analytics_job" "test" {
name = azurerm_stream_analytics_job.test.name
resource_group_name = azurerm_stream_analytics_job.test.resource_group_name

depends_on = [azurerm_stream_analytics_job_schedule.test]
}
`, config)
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/hashicorp/terraform-provider-azurerm/helpers/azure"
"github.com/hashicorp/terraform-provider-azurerm/helpers/tf"
"github.com/hashicorp/terraform-provider-azurerm/internal/clients"
"github.com/hashicorp/terraform-provider-azurerm/internal/locks"
"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/tags"
Expand Down Expand Up @@ -146,6 +147,9 @@ func resourceStreamAnalyticsJobCreateUpdate(d *pluginsdk.ResourceData, meta inte

id := parse.NewStreamingJobID(subscriptionId, d.Get("resource_group_name").(string), d.Get("name").(string))

locks.ByID(id.ID())
defer locks.UnlockByID(id.ID())

if d.IsNewResource() {
existing, err := client.Get(ctx, id.ResourceGroup, id.Name, "")
if err != nil {
Expand Down
Loading