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

tools/data-api-differ: supporting for diffing Common Types #4225

Merged
merged 7 commits into from
Jun 27, 2024
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0

package changes

var _ Change = CommonTypesApiVersionAdded{}

// CommonTypesApiVersionAdded defines information about a new API Version for CommonTypes.
type CommonTypesApiVersionAdded struct {
// ApiVersion specifies the API Version (e.g. `2023-01-01-preview`) for which CommonTypes have been added.
ApiVersion string
}

// IsBreaking returns whether this Change is considered a Breaking Change.
func (CommonTypesApiVersionAdded) IsBreaking() bool {
return false
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0

package changes

var _ Change = CommonTypesApiVersionRemoved{}

// CommonTypesApiVersionRemoved defines information about a new API Version for CommonTypes.
type CommonTypesApiVersionRemoved struct {
// ApiVersion specifies the API Version (e.g. `2023-01-01-preview`) for which CommonTypes have been removed.
ApiVersion string
}

// IsBreaking returns whether this Change is considered a Breaking Change.
func (CommonTypesApiVersionRemoved) IsBreaking() bool {
return true
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0

package changes

var _ Change = CommonTypesConstantAdded{}

// CommonTypesConstantAdded defines information about a new CommonTypes Constant.
type CommonTypesConstantAdded struct {
// ApiVersion specifies the name of the CommonTypes API Version which contains this Constant.
ApiVersion string

// ResourceName specifies the name of the API Resource which contains this Constant.
ResourceName string

// ConstantName specifies the name of the Constant which has been added.
ConstantName string

// ConstantType specifies the type of Constant (e.g. Int/String) that this is.
ConstantType string

// KeysAndValues specifies the Keys and Values for the Constant which has been added.
KeysAndValues map[string]string
}

// IsBreaking returns whether this Change is considered a Breaking Change.
func (CommonTypesConstantAdded) IsBreaking() bool {
return false
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0

package changes

var _ Change = CommonTypesConstantKeyValueAdded{}

// CommonTypesConstantKeyValueAdded specifies when a new Key/Value combination is added to an existing CommonTypes Constant.
type CommonTypesConstantKeyValueAdded struct {
// ApiVersion specifies the name of the API Version which contains this CommonTypes Constant.
ApiVersion string

// ConstantName specifies the name of the Constant which has been updated.
ConstantName string

// ConstantKey specifies the key for this new Constant Key/Value.
ConstantKey string

// ConstantValue specifies the value for this new Constant Key/Value.
ConstantValue string
}

// IsBreaking returns whether this Change is considered a Breaking Change.
func (CommonTypesConstantKeyValueAdded) IsBreaking() bool {
return false
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0

package changes

var _ Change = CommonTypesConstantKeyValueChanged{}

// CommonTypesConstantKeyValueChanged specifies when Constant Key has a new Value
type CommonTypesConstantKeyValueChanged struct {
// ApiVersion specifies the name of the API Version which contains this CommonTypes Constant.
ApiVersion string

// ConstantName specifies the name of the Constant which has an updated value.
ConstantName string

// ConstantKey specifies the key within the Constant which has changed.
ConstantKey string

// OldConstantValue specifies the old Value for this Constant Key.
OldConstantValue string

// NewConstantValue specifies the new/updated Value for this Constant Key.
NewConstantValue string
}

// IsBreaking returns whether this Change is considered a Breaking Change.
func (CommonTypesConstantKeyValueChanged) IsBreaking() bool {
return true
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0

package changes

var _ Change = CommonTypesConstantKeyValueRemoved{}

// CommonTypesConstantKeyValueRemoved specifies when a Key/Value combination is removed to an existing Constant.
type CommonTypesConstantKeyValueRemoved struct {
// ApiVersion specifies the name of the API Version which contains this CommonTypes Constant.
ApiVersion string

// ConstantName specifies the name of the Constant which has been updated.
ConstantName string

// ConstantKey specifies the key for the Constant Key/Value which has been removed.
ConstantKey string

// ConstantValue specifies the value for the Constant Key/Value which has been removed
ConstantValue string
}

// IsBreaking returns whether this Change is considered a Breaking Change.
func (CommonTypesConstantKeyValueRemoved) IsBreaking() bool {
return true
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0

package changes

var _ Change = CommonTypesConstantRemoved{}

// CommonTypesConstantRemoved defines information about a new CommonTypes Constant.
type CommonTypesConstantRemoved struct {
// ApiVersion specifies the name of the CommonTypes API Version which contains this Constant.
ApiVersion string

// ConstantName specifies the name of the Constant which has been removed.
ConstantName string

// ConstantType specifies the type of Constant (e.g. Int/String) that this is.
ConstantType string

// KeysAndValues specifies the Keys and Values for the Constant which has been removed.
KeysAndValues map[string]string
}

// IsBreaking returns whether this Change is considered a Breaking Change.
func (CommonTypesConstantRemoved) IsBreaking() bool {
return true
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0

package changes

var _ Change = CommonTypesConstantTypeChanged{}

// CommonTypesConstantTypeChanged specifies when a CommonTypes Constant has changed Type (e.g. `int` -> `string`)
type CommonTypesConstantTypeChanged struct {
// ApiVersion specifies the name of the API Version which contains this CommonTypes Constant.
ApiVersion string

// ConstantName specifies the name of the Constant which has changed.
ConstantName string

// OldType specifies the old type value for this Constant
OldType string

// NewType specifies the new/updated type value for this Constant
NewType string
}

// IsBreaking returns whether this Change is considered a Breaking Change.
func (c CommonTypesConstantTypeChanged) IsBreaking() bool {
// If a constant changes type, this is going to require code changes to account for this
return true
}
20 changes: 20 additions & 0 deletions tools/data-api-differ/internal/changes/common_types_model_added.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0

package changes

var _ Change = CommonTypesModelAdded{}

// CommonTypesModelAdded defines information about a new CommonTypes Model.
type CommonTypesModelAdded struct {
// ApiVersion specifies the name of the CommonTypes API Version which contains this Model.
ApiVersion string

// ModelName specifies the name of the Model which has been added.
ModelName string
}

// IsBreaking returns whether this Change is considered a Breaking Change.
func (CommonTypesModelAdded) IsBreaking() bool {
return false
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0

package changes

var _ Change = CommonTypesModelRemoved{}

// CommonTypesModelRemoved defines information about a CommonTypes Model which has been Removed.
type CommonTypesModelRemoved struct {
// ApiVersion specifies the name of the CommonTypes API Version which contained this Model.
ApiVersion string

// ModelName specifies the name of the Model which has been removed.
ModelName string
}

// IsBreaking returns whether this Change is considered a Breaking Change.
func (CommonTypesModelRemoved) IsBreaking() bool {
return true
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ type ConstantTypeChanged struct {
}

// IsBreaking returns whether this Change is considered a Breaking Change.

func (c ConstantTypeChanged) IsBreaking() bool {
// If a constant changes type, this is going to require code changes to account for this
return true
Expand Down
17 changes: 11 additions & 6 deletions tools/data-api-differ/internal/differ/differ.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ package differ

import (
"fmt"

"github.com/hashicorp/pandora/tools/data-api-differ/internal/changes"
"github.com/hashicorp/pandora/tools/data-api-differ/internal/log"
v1 "github.com/hashicorp/pandora/tools/data-api-sdk/v1"
Expand All @@ -18,13 +17,19 @@ func performDiff(initial, updated v1.LoadAllDataResult, includeNestedChangesWhen
diff := differ{}
output := make([]changes.Change, 0)

// TODO: support additional Data Sources when we're using the updated SDK
log.Logger.Trace("Detecting changes to the Resource Manager Services..")
resourceManagerChanges, err := diff.changesForServices(initial.Services, updated.Services, includeNestedChangesWhenNew)
log.Logger.Trace("Detecting changes to Common Types..")
commonTypesChanges, err := diff.changesForCommonTypes(initial.CommonTypes, updated.CommonTypes, includeNestedChangesWhenNew)
if err != nil {
return nil, fmt.Errorf("determining changes for the Common Types: %+v", err)
}
output = append(output, *commonTypesChanges...)

log.Logger.Trace("Detecting changes to the Services..")
serviceChanges, err := diff.changesForServices(initial.Services, updated.Services, includeNestedChangesWhenNew)
if err != nil {
return nil, fmt.Errorf("determining changes for the Resource Manager Services: %+v", err)
return nil, fmt.Errorf("determining changes for the Services: %+v", err)
}
output = append(output, *resourceManagerChanges...)
output = append(output, *serviceChanges...)

return &Result{
Changes: output,
Expand Down
22 changes: 1 addition & 21 deletions tools/data-api-differ/internal/differ/differ_api_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ package differ

import (
"fmt"
"sort"

"github.com/hashicorp/pandora/tools/data-api-differ/internal/changes"
"github.com/hashicorp/pandora/tools/data-api-differ/internal/log"
"github.com/hashicorp/pandora/tools/data-api-sdk/v1/models"
Expand All @@ -15,7 +13,7 @@ import (
// changesForApiResources determines the changes between the API Resources within the specified API Version.
func (d differ) changesForApiResources(serviceName, apiVersion string, initial, updated map[string]models.APIResource, includeNestedChangesWhenNew bool) (*[]changes.Change, error) {
output := make([]changes.Change, 0)
apiResources := d.uniqueApiResources(initial, updated)
apiResources := uniqueKeys(initial, updated)
for _, apiResource := range apiResources {
log.Logger.Trace(fmt.Sprintf("Detecting changes in API Resource %q..", apiResource))
changesForApiResource, err := d.changesForApiResource(serviceName, apiVersion, apiResource, initial, updated, includeNestedChangesWhenNew)
Expand Down Expand Up @@ -93,21 +91,3 @@ func (d differ) changesForApiResource(serviceName, apiVersion, apiResource strin

return &output, nil
}

// uniqueApiResources returns a unique, sorted list of API Resources from the keys of initial and updated.
func (d differ) uniqueApiResources(initial, updated map[string]models.APIResource) []string {
uniqueNames := make(map[string]struct{})
for name := range initial {
uniqueNames[name] = struct{}{}
}
for name := range updated {
uniqueNames[name] = struct{}{}
}

output := make([]string, 0)
for k := range uniqueNames {
output = append(output, k)
}
sort.Strings(output)
return output
}
22 changes: 1 addition & 21 deletions tools/data-api-differ/internal/differ/differ_api_versions.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ package differ

import (
"fmt"
"sort"

"github.com/hashicorp/pandora/tools/data-api-differ/internal/changes"
"github.com/hashicorp/pandora/tools/data-api-differ/internal/log"
"github.com/hashicorp/pandora/tools/data-api-sdk/v1/models"
Expand All @@ -15,7 +13,7 @@ import (
// changesForApiVersions determines the changes between the different API versions for the provided Service.
func (d differ) changesForApiVersions(serviceName string, initial, updated map[string]models.APIVersion, includeNestedChangesWhenNew bool) (*[]changes.Change, error) {
output := make([]changes.Change, 0)
apiVersions := d.uniqueApiVersions(initial, updated)
apiVersions := uniqueKeys(initial, updated)
for _, apiVersion := range apiVersions {
log.Logger.Trace(fmt.Sprintf("Detecting changes in API Version %q..", apiVersion))
changesForApiVersion, err := d.changesForApiVersion(serviceName, apiVersion, initial, updated, includeNestedChangesWhenNew)
Expand Down Expand Up @@ -66,21 +64,3 @@ func (d differ) changesForApiVersion(serviceName, apiVersion string, initial, up

return &output, nil
}

// uniqueApiVersions returns a unique, sorted list of API Versions from the keys of initial and updated.
func (d differ) uniqueApiVersions(initial, updated map[string]models.APIVersion) []string {
uniqueNames := make(map[string]struct{})
for name := range initial {
uniqueNames[name] = struct{}{}
}
for name := range updated {
uniqueNames[name] = struct{}{}
}

output := make([]string, 0)
for k := range uniqueNames {
output = append(output, k)
}
sort.Strings(output)
return output
}
Loading
Loading