azure-sdk-for-python icon indicating copy to clipboard operation
azure-sdk-for-python copied to clipboard

custom_headers parameter to NetworkInterfacesOperations._create_or_update_initial

Open NCarter3 opened this issue 3 years ago • 11 comments

Is your feature request related to a problem? Please describe. In working with Azure on building a network integration, we've been asked to add a header to requests. To use the SDK and send custom headers requires some patching to get to work right now.

Describe the solution you'd like I'd like a "custom_headers: Optional[Mapping[str, str]]" parameter on NetworkInterfacesOperations._create_or_update_initial that matches behavior to other Operations classes in the SDK.

Describe alternatives you've considered Custom patching of the SDK.

Graphrbac is an example of another client with this feature already implemented: see sdk/graphrbac/azure-graphrbac/azure/graphrbac/operations/users_operations.py:73:

    def create(
            self, parameters, custom_headers=None, raw=False, **operation_config):
...
        :param dict custom_headers: headers that will be added to the request
...
        if custom_headers:
            header_parameters.update(custom_headers)

NCarter3 avatar Jul 18 '22 21:07 NCarter3

Label prediction was below confidence level 0.6 for Model:ServiceLabels: 'Graph:0.38314182,Storage:0.052597385,AAD:0.05146199'

azure-sdk avatar Jul 18 '22 21:07 azure-sdk

Label prediction was below confidence level 0.6 for Model:ServiceLabels: 'Graph:0.38314182,Storage:0.052597385,AAD:0.05146199'

azure-sdk avatar Jul 20 '22 03:07 azure-sdk

@NCarter3 thank you for the issue report. @msyyc can you take a look?

tjprescott avatar Jul 20 '22 04:07 tjprescott

Hi @Wzb123456789 Please help on this issue

msyyc avatar Jul 21 '22 07:07 msyyc

Hi @NCarter3

Please provide relevant code about what you want to build a network integration to better solve your problem.

Wzb123456789 avatar Jul 26 '22 09:07 Wzb123456789

Here's an outline of the changes I made to patch in support the custom headers, based on the PIP package azure-mgmt-network==19.0.0 (with some extra changes to conform to our internal style guide). I've omitted most of the methods that don't deal with headers.

Since posting, I realized we also need to do this when deleting NIC resources, so adding the same flexibility to NetworkInterfacesOperations._delete_initial is appreciated!

class ExtendedNetworkInterfaceOperations(NetworkInterfacesOperations):
    def _create_or_update_initial(
        self,
        resource_group_name: str,
        network_interface_name: str,
        parameters: object,
        custom_headers: Optional[Mapping[str, str]] = None,
        **kwargs: Any,
    ) -> network_models.NetworkInterface:
...
        # Construct headers
        header_parameters = {}  # type: Dict[str, Any]
        header_parameters['Content-Type'] = self._serialize.header(
            'content_type', content_type, 'str'
        )
        header_parameters['Accept'] = self._serialize.header('accept', accept, 'str')
        if custom_headers:
            header_parameters.update(custom_headers)
...
        request = self._client.put(url, query_parameters, header_parameters, **body_content_kwargs)
...


    def _delete_initial(
        self,
        resource_group_name: str,
        network_interface_name: str,
        custom_headers: Optional[Mapping[str, str]] = None,
        **kwargs: Any,
    ) -> None:
...
        # Construct headers
        header_parameters = {}  # type: Dict[str, Any]
        header_parameters['Accept'] = self._serialize.header('accept', accept, 'str')
        if custom_headers:
            header_parameters.update(custom_headers)
...
        request = self._client.delete(url, query_parameters, header_parameters)
...

Then, once an appropriate client is constructed that uses the patched ExtendedNetworkInterfaceOperations, custom_headers can be provided:

        network_management_client.network_interfaces.begin_create_or_update(
            resource_group_name=resource_group,
            network_interface_name=name,
            parameters=nic,
            custom_headers={
                'x-ms-required-header': 'foo',
                'x-ms-correlation-id': 'test',
            },

NCarter3 avatar Jul 26 '22 23:07 NCarter3

Hi @NCarter3, thanks for your feedback.

We are about to release a new package of azure-mgmt-network, which contains the parameters of custom headers, which you can use according to your own needs.

Wzb123456789 avatar Aug 01 '22 02:08 Wzb123456789

Hi @NCarter3

Please check if the headers defined like this match your expectations:

res = network_client.network_interfaces.begin_create_or_update(
    resource_group_name='resource_group_name',
    network_interface_name='network_interface_name',
    parameters='nic',
    headers={
        'x-ms-required-header': 'foo',
        'x-ms-correlation-id': 'test',
    },
)

Please let me know if it meets your expectations, we will release a new version of azure-mgmt-network as soon as possible

Wzb123456789 avatar Aug 04 '22 06:08 Wzb123456789

That looks great! And I assume the same for begin_delete:

res = network_client.network_interfaces.begin_delete(
    resource_group_name='resource_group_name',
    network_interface_name='network_interface_name',
    headers={
        'x-ms-required-header': 'foo',
        'x-ms-correlation-id': 'test',
    },
)

Thank you much!

NCarter3 avatar Aug 04 '22 23:08 NCarter3

Hi @NCarter3. Thank you for opening this issue and giving us the opportunity to assist. We believe that this has been addressed. If you feel that further discussion is needed, please add a comment with the text “/unresolve” to remove the “issue-addressed” label and continue the conversation.

ghost avatar Aug 05 '22 19:08 ghost

Hi @NCarter3

The new azure-mgmt-network package has been released, please try to use the new package to solve your problem. https://pypi.org/project/azure-mgmt-network/21.0.0/

Wzb123456789 avatar Aug 08 '22 05:08 Wzb123456789

Hi @NCarter3, since you haven’t asked that we “/unresolve” the issue, we’ll close this out. If you believe further discussion is needed, please add a comment “/unresolve” to reopen the issue.

ghost avatar Aug 15 '22 10:08 ghost

Hi @Wzb123456789

I had a chance to try the new package, and it's almost perfect! Thanks for releasing this! There's one remaining edge that I see -- my headers are not in the status requests that LROPoller makes. If I'm reading the code accurately, the headers are popped out of the kwargs now. This means those headers are not present in the "operation_config" passed to ARMPolling/LROPoller. I need to pass the headers along in these requests too.

/unresolve

NCarter3 avatar Aug 26 '22 17:08 NCarter3

Hi @NCarter3

According to my investigation, polling is by default, your polling method will be ARMPolling. Pass in False for this operation to not poll, or pass in your own initialized polling object for a personal polling strategy.

Wzb123456789 avatar Aug 31 '22 07:08 Wzb123456789

Hi @NCarter3. Thank you for opening this issue and giving us the opportunity to assist. We believe that this has been addressed. If you feel that further discussion is needed, please add a comment with the text “/unresolve” to remove the “issue-addressed” label and continue the conversation.

ghost avatar Sep 02 '22 21:09 ghost

Yep, that's what I ended up doing. It just feels a little awkward, and doesn't match what I would have expected. Thanks for making it possible!

NCarter3 avatar Sep 02 '22 22:09 NCarter3