Skip to content

Commit

Permalink
Fixed use of deprecated attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
tkielar-pgs committed Oct 28, 2022
1 parent 5a6a099 commit a94c97c
Show file tree
Hide file tree
Showing 37 changed files with 276 additions and 321 deletions.
30 changes: 15 additions & 15 deletions internal/planModifiers/ignoreCaseModifier_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,38 +16,38 @@ func TestIgnoreCaseModifier(t *testing.T) {
}{
"empty state": {
request: tfsdk.ModifyAttributePlanRequest{
AttributeState: types.String{Unknown: true},
AttributePlan: types.String{Value: "plannedValue"},
AttributeState: types.StringUnknown(),
AttributePlan: types.StringValue("plannedValue"),
},
expectedValue: types.String{Value: "plannedValue"},
expectedValue: types.StringValue("plannedValue"),
},
"empty plan": {
request: tfsdk.ModifyAttributePlanRequest{
AttributeState: types.String{Value: "stateValue"},
AttributePlan: types.String{Null: true},
AttributeState: types.StringValue("stateValue"),
AttributePlan: types.StringNull(),
},
expectedValue: types.String{Null: true},
expectedValue: types.StringNull(),
},
"non string": {
request: tfsdk.ModifyAttributePlanRequest{
AttributeState: types.Int64{Value: 246},
AttributePlan: types.Int64{Value: 45763},
AttributeState: types.Int64Value(246),
AttributePlan: types.Int64Value(45763),
},
expectedValue: types.Int64{Value: 45763},
expectedValue: types.Int64Value(45763),
},
"matching case": {
request: tfsdk.ModifyAttributePlanRequest{
AttributeState: types.String{Value: "matchingCase"},
AttributePlan: types.String{Value: "matchingCase"},
AttributeState: types.StringValue("matchingCase"),
AttributePlan: types.StringValue("matchingCase"),
},
expectedValue: types.String{Value: "matchingCase"},
expectedValue: types.StringValue("matchingCase"),
},
"not matching case": {
request: tfsdk.ModifyAttributePlanRequest{
AttributeState: types.String{Value: "NotMatchingCase"},
AttributePlan: types.String{Value: "NOTMATCHINGCASE"},
AttributeState: types.StringValue("NotMatchingCase"),
AttributePlan: types.StringValue("NOTMATCHINGCASE"),
},
expectedValue: types.String{Value: "NotMatchingCase"},
expectedValue: types.StringValue("NotMatchingCase"),
},
}

Expand Down
4 changes: 2 additions & 2 deletions internal/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,8 +176,8 @@ func (p *mssqlProvider) ValidateConfig(ctx context.Context, request provider.Val
utils.StopOnError(ctx).
Then(func() { data = utils.GetData[providerData](ctx, request.Config) }).
Then(func() {
if data.AzureAuth.IsNull() && data.SqlAuth.IsNull() {
if data.AzureAuth == nil && data.SqlAuth == nil {
utils.AddError(ctx, "Missing SQL authentication config", errors.New("One of authentication methods must be provided: sql_auth, azure_auth"))
}
})
}
}
44 changes: 19 additions & 25 deletions internal/provider/providerData.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,70 +23,64 @@ type azureAuth struct {
type providerData struct {
Hostname types.String `tfsdk:"hostname"`
Port types.Int64 `tfsdk:"port"`
SqlAuth types.Object `tfsdk:"sql_auth"`
AzureAuth types.Object `tfsdk:"azure_auth"`
SqlAuth *sqlAuth `tfsdk:"sql_auth"`
AzureAuth *azureAuth `tfsdk:"azure_auth"`
}

func (pd providerData) asConnectionDetails(ctx context.Context) (sql.ConnectionDetails, diag.Diagnostics) {
func (pd providerData) asConnectionDetails(context.Context) (sql.ConnectionDetails, diag.Diagnostics) {
diags := diag.Diagnostics{}

var addComputedError = func(summary string) {
diags.AddError(summary, "SQL connection details must be known during plan execution")
}

if pd.Hostname.Unknown {
if pd.Hostname.IsUnknown() {
addComputedError("Hostname cannot be a computed value")
}

connDetails := sql.ConnectionDetails{
Host: os.Getenv("MSSQL_HOSTNAME"),
}

if !pd.Hostname.Null {
connDetails.Host = pd.Hostname.Value
if !pd.Hostname.IsNull() {
connDetails.Host = pd.Hostname.ValueString()
}

if !pd.Port.Null {
connDetails.Host = fmt.Sprintf("%s:%d", connDetails.Host, pd.Port.Value)
if !pd.Port.IsNull() {
connDetails.Host = fmt.Sprintf("%s:%d", connDetails.Host, pd.Port.ValueInt64())
} else if envPort := os.Getenv("MSSQL_PORT"); envPort != "" {
connDetails.Host = fmt.Sprintf("%s:%s", connDetails.Host, envPort)
}

if !pd.SqlAuth.Null {
var auth sqlAuth
diags.Append(pd.SqlAuth.As(ctx, &auth, types.ObjectAsOptions{})...)

if auth.Username.Unknown {
if pd.SqlAuth != nil {
if pd.SqlAuth.Username.IsUnknown() {
addComputedError("SQL username cannot be a computed value")
}

if auth.Password.Unknown {
if pd.SqlAuth.Password.IsUnknown() {
addComputedError("SQL password cannot be a computed value")
}

connDetails.Auth = sql.ConnectionAuthSql{Username: auth.Username.Value, Password: auth.Password.Value}
connDetails.Auth = sql.ConnectionAuthSql{Username: pd.SqlAuth.Username.ValueString(), Password: pd.SqlAuth.Password.ValueString()}
}

if !pd.AzureAuth.Null {
var auth azureAuth
diags.Append(pd.AzureAuth.As(ctx, &auth, types.ObjectAsOptions{})...)

if auth.ClientId.Unknown {
if pd.AzureAuth != nil {
if pd.AzureAuth.ClientId.IsUnknown() {
addComputedError("Azure AD Service Principal client_id cannot be a computed value")
}

if auth.ClientSecret.Unknown {
if pd.AzureAuth.ClientSecret.IsUnknown() {
addComputedError("Azure AD Service Principal client_secret cannot be a computed value")
}

if auth.TenantId.Unknown {
if pd.AzureAuth.TenantId.IsUnknown() {
addComputedError("Azure AD Service Principal tenant_id cannot be a computed value")
}

connAuth := sql.ConnectionAuthAzure{
ClientId: auth.ClientId.Value,
ClientSecret: auth.ClientSecret.Value,
TenantId: auth.TenantId.Value,
ClientId: pd.AzureAuth.ClientId.ValueString(),
ClientSecret: pd.AzureAuth.ClientSecret.ValueString(),
TenantId: pd.AzureAuth.TenantId.ValueString(),
}

if connAuth.ClientId == "" {
Expand Down
Loading

0 comments on commit a94c97c

Please sign in to comment.