@@ -122,15 +122,7 @@ def _create_code_model(self, yaml_data: Dict[str, Any], options: Dict[str, Union
122122
123123 return code_model
124124
125- def _build_code_model_options (self ) -> Dict [str , Any ]:
126- """Build en options dict from the user input while running autorest.
127- """
128- azure_arm = self ._autorestapi .get_boolean_value ("azure-arm" , False )
129- credential = (
130- self ._autorestapi .get_boolean_value ("add-credentials" , False ) or
131- self ._autorestapi .get_boolean_value ("add-credential" , False )
132- )
133-
125+ def _get_credential_scopes (self , credential ):
134126 credential_scopes_temp = self ._autorestapi .get_value ("credential-scopes" )
135127 credential_scopes = credential_scopes_temp .split ("," ) if credential_scopes_temp else None
136128 if credential_scopes and not credential :
@@ -142,6 +134,50 @@ def _build_code_model_options(self) -> Dict[str, Any]:
142134 "--credential-scopes takes a list of scopes in comma separated format. "
143135 "For example: --credential-scopes=https://cognitiveservices.azure.com/.default"
144136 )
137+ return credential_scopes
138+
139+ def _get_credential_param (self , azure_arm , credential , credential_default_policy_type ):
140+ credential_scopes = self ._get_credential_scopes (credential )
141+ credential_key_header_name = self ._autorestapi .get_value ('credential-key-header-name' )
142+
143+ if credential_default_policy_type == "BearerTokenCredentialPolicy" :
144+ if not credential_scopes :
145+ if azure_arm :
146+ credential_scopes = ["https://management.azure.com/.default" ]
147+ elif credential :
148+ # If add-credential is specified, we still want to add a credential_scopes variable.
149+ # Will make it an empty list so we can differentiate between this case and None
150+ _LOGGER .warning (
151+ "You have used the --add-credential flag but not the --credential-scopes flag "
152+ "while generating non-management plane code. "
153+ "This is not recommend because it forces the customer to pass credential scopes "
154+ "through kwargs if they want to authenticate."
155+ )
156+ credential_scopes = []
157+ if credential_key_header_name :
158+ raise ValueError (
159+ "You have passed in a credential key header name with default credential policy type "
160+ "BearerTokenCredentialPolicy. This is not allowed, since credential key header name is tied with "
161+ "AzureKeyCredentialPolicy. Instead, with this policy it is recommend you pass in "
162+ "--credential-scopes."
163+ )
164+ else :
165+ # currently the only other credential policy is AzureKeyCredentialPolicy
166+ if credential_scopes :
167+ raise ValueError (
168+ "You have passed in credential scopes with default credential policy type "
169+ "AzureKeyCredentialPolicy. This is not allowed, since credential scopes is tied with "
170+ "BearerTokenCredentialPolicy. Instead, with this policy you must pass in "
171+ "--credential-key-header-name."
172+ )
173+ if not credential_key_header_name :
174+ raise ValueError (
175+ "With default credential policy type AzureKeyCredentialPolicy, you must pass in the name "
176+ "of the key header with the flag --credential-key-header-name"
177+ )
178+ return credential_scopes , credential_key_header_name
179+
180+ def _handle_default_authentication_policy (self , azure_arm , credential ):
145181
146182 passed_in_credential_default_policy_type = (
147183 self ._autorestapi .get_value ("credential-default-policy-type" ) or "BearerTokenCredentialPolicy"
@@ -159,27 +195,27 @@ def _build_code_model_options(self) -> Dict[str, Any]:
159195 "BearerTokenCredentialPolicy or AzureKeyCredentialPolicy"
160196 )
161197
162- if credential_scopes and credential_default_policy_type != "BearerTokenCredentialPolicy" :
163- _LOGGER .warning (
164- "You have --credential-default-policy-type not set as BearerTokenCredentialPolicy and a value for "
165- "--credential-scopes. Since credential scopes are tied to the BearerTokenCredentialPolicy, "
166- "we will ignore your credential scopes."
198+ credential_scopes , credential_key_header_name = self ._get_credential_param (
199+ azure_arm , credential , credential_default_policy_type
200+ )
201+
202+ return credential_default_policy_type , credential_scopes , credential_key_header_name
203+
204+
205+ def _build_code_model_options (self ) -> Dict [str , Any ]:
206+ """Build en options dict from the user input while running autorest.
207+ """
208+ azure_arm = self ._autorestapi .get_boolean_value ("azure-arm" , False )
209+ credential = (
210+ self ._autorestapi .get_boolean_value ("add-credentials" , False ) or
211+ self ._autorestapi .get_boolean_value ("add-credential" , False )
212+ )
213+
214+ credential_default_policy_type , credential_scopes , credential_key_header_name = (
215+ self ._handle_default_authentication_policy (
216+ azure_arm , credential
167217 )
168- credential_scopes = []
169-
170- elif not credential_scopes and credential_default_policy_type == "BearerTokenCredentialPolicy" :
171- if azure_arm :
172- credential_scopes = ["https://management.azure.com/.default" ]
173- elif credential :
174- # If add-credential is specified, we still want to add a credential_scopes variable.
175- # Will make it an empty list so we can differentiate between this case and None
176- _LOGGER .warning (
177- "You have used the --add-credential flag but not the --credential-scopes flag "
178- "while generating non-management plane code. "
179- "This is not recommend because it forces the customer to pass credential scopes "
180- "through kwargs if they want to authenticate."
181- )
182- credential_scopes = []
218+ )
183219
184220
185221 license_header = self ._autorestapi .get_value ("header-text" )
@@ -194,6 +230,7 @@ def _build_code_model_options(self) -> Dict[str, Any]:
194230 "azure_arm" : azure_arm ,
195231 "credential" : credential ,
196232 "credential_scopes" : credential_scopes ,
233+ "credential_key_header_name" : credential_key_header_name ,
197234 "head_as_boolean" : self ._autorestapi .get_boolean_value ("head-as-boolean" , False ),
198235 "license_header" : license_header ,
199236 "keep_version_file" : self ._autorestapi .get_boolean_value ("keep-version-file" , False ),
0 commit comments