@@ -8,22 +8,25 @@ import (
88 "log"
99 "time"
1010
11+ "github.com/hashicorp/go-azure-helpers/lang/pointer"
1112 "github.com/hashicorp/go-azure-helpers/lang/response"
1213 "github.com/hashicorp/go-azure-sdk/resource-manager/sql/2023-08-01-preview/jobcredentials"
14+ "github.com/hashicorp/go-cty/cty"
15+ "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
16+ "github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
1317 "github.com/hashicorp/terraform-provider-azurerm/helpers/tf"
1418 "github.com/hashicorp/terraform-provider-azurerm/internal/clients"
1519 "github.com/hashicorp/terraform-provider-azurerm/internal/services/mssql/parse"
1620 "github.com/hashicorp/terraform-provider-azurerm/internal/services/mssql/validate"
1721 "github.com/hashicorp/terraform-provider-azurerm/internal/tf/pluginsdk"
1822 "github.com/hashicorp/terraform-provider-azurerm/internal/timeouts"
19- "github.com/hashicorp/terraform-provider-azurerm/utils"
2023)
2124
2225func resourceMsSqlJobCredential () * pluginsdk.Resource {
2326 return & pluginsdk.Resource {
24- Create : resourceMsSqlJobCredentialCreateUpdate ,
27+ Create : resourceMsSqlJobCredentialCreate ,
2528 Read : resourceMsSqlJobCredentialRead ,
26- Update : resourceMsSqlJobCredentialCreateUpdate ,
29+ Update : resourceMsSqlJobCredentialUpdate ,
2730 Delete : resourceMsSqlJobCredentialDelete ,
2831
2932 Importer : pluginsdk .ImporterValidatingResourceId (func (id string ) error {
@@ -38,6 +41,10 @@ func resourceMsSqlJobCredential() *pluginsdk.Resource {
3841 Delete : pluginsdk .DefaultTimeout (60 * time .Minute ),
3942 },
4043
44+ ValidateRawResourceConfigFuncs : []schema.ValidateRawResourceConfigFunc {
45+ validation .PreferWriteOnlyAttribute (cty .GetAttrPath ("password" ), cty .GetAttrPath ("password_wo" )),
46+ },
47+
4148 Schema : map [string ]* pluginsdk.Schema {
4249 "name" : {
4350 Type : pluginsdk .TypeString ,
@@ -58,17 +65,32 @@ func resourceMsSqlJobCredential() *pluginsdk.Resource {
5865 },
5966
6067 "password" : {
61- Type : pluginsdk .TypeString ,
62- Required : true ,
63- Sensitive : true ,
68+ Type : pluginsdk .TypeString ,
69+ Optional : true ,
70+ Sensitive : true ,
71+ ConflictsWith : []string {"password_wo" },
72+ ExactlyOneOf : []string {"password" , "password_wo" },
73+ },
74+ "password_wo" : {
75+ Type : pluginsdk .TypeString ,
76+ Optional : true ,
77+ WriteOnly : true ,
78+ RequiredWith : []string {"password_wo_version" },
79+ ConflictsWith : []string {"password" },
80+ ExactlyOneOf : []string {"password_wo" , "password" },
81+ },
82+ "password_wo_version" : {
83+ Type : pluginsdk .TypeInt ,
84+ Optional : true ,
85+ RequiredWith : []string {"password_wo" },
6486 },
6587 },
6688 }
6789}
6890
69- func resourceMsSqlJobCredentialCreateUpdate (d * pluginsdk.ResourceData , meta interface {}) error {
91+ func resourceMsSqlJobCredentialCreate (d * pluginsdk.ResourceData , meta interface {}) error {
7092 client := meta .(* clients.Client ).MSSQL .JobCredentialsClient
71- ctx , cancel := timeouts .ForCreateUpdate (meta .(* clients.Client ).StopContext , d )
93+ ctx , cancel := timeouts .ForCreate (meta .(* clients.Client ).StopContext , d )
7294 defer cancel ()
7395
7496 log .Printf ("[INFO] preparing arguments for Job Credential creation." )
@@ -79,39 +101,95 @@ func resourceMsSqlJobCredentialCreateUpdate(d *pluginsdk.ResourceData, meta inte
79101 }
80102 jobCredentialId := jobcredentials .NewCredentialID (jaId .SubscriptionId , jaId .ResourceGroupName , jaId .ServerName , jaId .JobAgentName , d .Get ("name" ).(string ))
81103
82- username := d .Get ("username" ).(string )
83- password := d .Get ("password" ).(string )
104+ existing , err := client .Get (ctx , jobCredentialId )
105+ if err != nil && ! response .WasNotFound (existing .HttpResponse ) {
106+ return fmt .Errorf ("checking for presence of existing %s: %+v" , jobCredentialId , err )
107+ }
84108
85- if d .IsNewResource () {
86- existing , err := client .Get (ctx , jobCredentialId )
87- if err != nil {
88- if ! response .WasNotFound (existing .HttpResponse ) {
89- return fmt .Errorf ("checking for presence of existing MsSql %s: %+v" , jobCredentialId , err )
90- }
91- }
109+ if ! response .WasNotFound (existing .HttpResponse ) {
110+ return tf .ImportAsExistsError ("azurerm_mssql_job_credential" , jobCredentialId .ID ())
111+ }
92112
93- if ! response .WasNotFound (existing .HttpResponse ) {
94- return tf .ImportAsExistsError ("azurerm_mssql_job_credential" , jobCredentialId .ID ())
95- }
113+ woPassword , err := pluginsdk .GetWriteOnly (d , "password_wo" , cty .String )
114+ if err != nil {
115+ return err
116+ }
117+
118+ password := d .Get ("password" ).(string )
119+ if ! woPassword .IsNull () {
120+ password = woPassword .AsString ()
96121 }
97122
98123 jobCredential := jobcredentials.JobCredential {
99- Name : utils . String (jobCredentialId .CredentialName ),
124+ Name : pointer . To (jobCredentialId .CredentialName ),
100125 Properties : & jobcredentials.JobCredentialProperties {
101- Username : username ,
126+ Username : d . Get ( " username" ).( string ) ,
102127 Password : password ,
103128 },
104129 }
105130
106131 if _ , err := client .CreateOrUpdate (ctx , jobCredentialId , jobCredential ); err != nil {
107- return fmt .Errorf ("creating MsSql %s: %+v" , jobCredentialId , err )
132+ return fmt .Errorf ("creating %s: %+v" , jobCredentialId , err )
108133 }
109134
110135 d .SetId (jobCredentialId .ID ())
111136
112137 return resourceMsSqlJobCredentialRead (d , meta )
113138}
114139
140+ func resourceMsSqlJobCredentialUpdate (d * pluginsdk.ResourceData , meta interface {}) error {
141+ client := meta .(* clients.Client ).MSSQL .JobCredentialsClient
142+ ctx , cancel := timeouts .ForUpdate (meta .(* clients.Client ).StopContext , d )
143+ defer cancel ()
144+
145+ log .Printf ("[INFO] preparing arguments for Job Credential update." )
146+
147+ jaId , err := jobcredentials .ParseJobAgentID (d .Get ("job_agent_id" ).(string ))
148+ if err != nil {
149+ return err
150+ }
151+ jobCredentialId := jobcredentials .NewCredentialID (jaId .SubscriptionId , jaId .ResourceGroupName , jaId .ServerName , jaId .JobAgentName , d .Get ("name" ).(string ))
152+
153+ existing , err := client .Get (ctx , jobCredentialId )
154+ if err != nil {
155+ return fmt .Errorf ("retrieving %s: %+v" , jobCredentialId , err )
156+ }
157+
158+ if existing .Model == nil {
159+ return fmt .Errorf ("retrieving %s: `model` was nil" , jobCredentialId )
160+ }
161+
162+ if existing .Model .Properties == nil {
163+ return fmt .Errorf ("retrieving %s: `model.Properties` was nil" , jobCredentialId )
164+ }
165+ payload := existing .Model
166+
167+ if d .HasChange ("username" ) {
168+ payload .Properties .Username = d .Get ("username" ).(string )
169+ }
170+
171+ if d .HasChange ("password" ) {
172+ payload .Properties .Password = d .Get ("password" ).(string )
173+ }
174+
175+ if d .HasChange ("password_wo_version" ) {
176+ woPassword , err := pluginsdk .GetWriteOnly (d , "password_wo" , cty .String )
177+ if err != nil {
178+ return err
179+ }
180+
181+ if ! woPassword .IsNull () {
182+ payload .Properties .Password = woPassword .AsString ()
183+ }
184+ }
185+
186+ if _ , err := client .CreateOrUpdate (ctx , jobCredentialId , * payload ); err != nil {
187+ return fmt .Errorf ("updating %s: %+v" , jobCredentialId , err )
188+ }
189+
190+ return resourceMsSqlJobCredentialRead (d , meta )
191+ }
192+
115193func resourceMsSqlJobCredentialRead (d * pluginsdk.ResourceData , meta interface {}) error {
116194 client := meta .(* clients.Client ).MSSQL .JobCredentialsClient
117195 ctx , cancel := timeouts .ForRead (meta .(* clients.Client ).StopContext , d )
@@ -140,6 +218,9 @@ func resourceMsSqlJobCredentialRead(d *pluginsdk.ResourceData, meta interface{})
140218 d .Set ("username" , props .Username )
141219 }
142220 }
221+
222+ d .Set ("password_wo_version" , d .Get ("password_wo_version" ).(int ))
223+
143224 return nil
144225}
145226
0 commit comments