Repository: ansible/ansible

Issue:
azure_rm_networkinterface should have support to enable accelerated networking  for network interface.
<!---
Verify first that your issue/request is not already reported on GitHub.
THIS FORM WILL BE READ BY A MACHINE, COMPLETE ALL SECTIONS AS DESCRIBED.
Also test if the latest release, and devel branch are affected too.
ALWAYS add information AFTER (OUTSIDE) these html comments.
Otherwise it may end up being automatically closed by our bot. -->

##### SUMMARY
<!--- Explain the problem briefly -->

Current module does not have support for following features while a NetworkInterface gets created.

    NetworkInterface
    Accelerated networking flag to enable/disable SR-IOV based networking.

New APIs supporting these features are available in Azure Python SDK.
(e.g. https://docs.microsoft.com/ja-jp/python/api/azure.mgmt.network.v2018_02_01.models.networkinterface?view=azure-python)

NetworkInterface(*, id: str = None, location: str = None, tags=None, virtual_machine=None, network_security_group=None, ip_configurations=None, dns_settings=None, mac_address: str = None, primary: bool = None, **enable_accelerated_networking**: bool = None, enable_ip_forwarding: bool = None, resource_guid: str = None, provisioning_state: str = None, etag: str = None, **kwargs) -> None

There is already a similar issue.
https://github.com/ansible/ansible/issues/34032
##### ISSUE TYPE
 - Feature Idea

##### COMPONENT NAME
<!--- Insert, BELOW THIS COMMENT, the name of the module, plugin, task or feature.
Do not include extra details here, e.g. "vyos_command" not "the network module vyos_command" or the full path-->
azure_rm_networkinterface
##### ANSIBLE VERSION
<!--- Paste, BELOW THIS COMMENT, verbatim output from "ansible --version" between quotes below -->
```
$ ansible --version
ansible 2.5.4
  config file = None
  configured module search path = ['/home/ubuntu/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/local/lib/python3.6/dist-packages/ansible
  executable location = /usr/local/bin/ansible
  python version = 3.6.5 (default, Apr  1 2018, 05:46:30) [GCC 7.3.0]
```

##### OS / ENVIRONMENT
<!--- Mention, BELOW THIS COMMENT, the OS you are running Ansible from, and the OS you are
managing, or say "N/A" for anything that is not platform-specific.
Also mention the specific version of what you are trying to control,
e.g. if this is a network bug the version of firmware on the network device.-->
Red Hat Enterprise Linux Server release 7.5 (Maipo)
##### STEPS TO REPRODUCE
<!--- For bugs, show exactly how to reproduce the problem, using a minimal test-case.
For new features, show how the feature would be used. -->

<!--- Paste example playbooks or commands between quotes below -->
```yaml
- name: Create a network interface with private IP address only (no Public IP)
  azure_rm_networkinterface:
    name: nic001
    resource_group: Testing
    virtual_network_name: vnet001
    subnet_name: subnet001
    enable_accelerated_networking: True
```



azure_rm_networkinterface should have support to enable accelerated networking  for network interface.
<!---
Verify first that your issue/request is not already reported on GitHub.
THIS FORM WILL BE READ BY A MACHINE, COMPLETE ALL SECTIONS AS DESCRIBED.
Also test if the latest release, and devel branch are affected too.
ALWAYS add information AFTER (OUTSIDE) these html comments.
Otherwise it may end up being automatically closed by our bot. -->

##### SUMMARY
<!--- Explain the problem briefly -->

Current module does not have support for following features while a NetworkInterface gets created.

    NetworkInterface
    Accelerated networking flag to enable/disable SR-IOV based networking.

New APIs supporting these features are available in Azure Python SDK.
(e.g. https://docs.microsoft.com/ja-jp/python/api/azure.mgmt.network.v2018_02_01.models.networkinterface?view=azure-python)

NetworkInterface(*, id: str = None, location: str = None, tags=None, virtual_machine=None, network_security_group=None, ip_configurations=None, dns_settings=None, mac_address: str = None, primary: bool = None, **enable_accelerated_networking**: bool = None, enable_ip_forwarding: bool = None, resource_guid: str = None, provisioning_state: str = None, etag: str = None, **kwargs) -> None

There is already a similar issue.
https://github.com/ansible/ansible/issues/34032
##### ISSUE TYPE
 - Feature Idea

##### COMPONENT NAME
<!--- Insert, BELOW THIS COMMENT, the name of the module, plugin, task or feature.
Do not include extra details here, e.g. "vyos_command" not "the network module vyos_command" or the full path-->
azure_rm_networkinterface
##### ANSIBLE VERSION
<!--- Paste, BELOW THIS COMMENT, verbatim output from "ansible --version" between quotes below -->
```
$ ansible --version
ansible 2.5.4
  config file = None
  configured module search path = ['/home/ubuntu/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/local/lib/python3.6/dist-packages/ansible
  executable location = /usr/local/bin/ansible
  python version = 3.6.5 (default, Apr  1 2018, 05:46:30) [GCC 7.3.0]
```

##### OS / ENVIRONMENT
<!--- Mention, BELOW THIS COMMENT, the OS you are running Ansible from, and the OS you are
managing, or say "N/A" for anything that is not platform-specific.
Also mention the specific version of what you are trying to control,
e.g. if this is a network bug the version of firmware on the network device.-->
Red Hat Enterprise Linux Server release 7.5 (Maipo)
##### STEPS TO REPRODUCE
<!--- For bugs, show exactly how to reproduce the problem, using a minimal test-case.
For new features, show how the feature would be used. -->

<!--- Paste example playbooks or commands between quotes below -->
```yaml
- name: Create a network interface with private IP address only (no Public IP)
  azure_rm_networkinterface:
    name: nic001
    resource_group: Testing
    virtual_network_name: vnet001
    subnet_name: subnet001
    enable_accelerated_networking: True
```





Discussion:
Files identified in the description:
* [lib/ansible/modules/cloud/azure/azure_rm_networkinterface.py](https://github.com/ansible/ansible/blob/devel/lib/ansible/modules/cloud/azure/azure_rm_networkinterface.py)

If these files are inaccurate, please update the `component name` section of the description or use the `!component` bot command.

[click here for bot help](https://github.com/ansible/ansibullbot/blob/master/ISSUE_HELP.md)
<!--- boilerplate: components_banner --->
cc @haroldwongms @nitzmahone @trstringer @xscript @yuwzho @zikalino
[click here for bot help](https://github.com/ansible/ansibullbot/blob/master/ISSUE_HELP.md)
<!--- boilerplate: notify --->
+label easyfix

add an easyfix label to attract contributor who is interested in this field. Add a new field need to do the following things:

- Update document at the beginning of the module python file. Including a `version_added: 2.7` line
- Add this to spec in `init()`
- Handle new created case in `exec_module()`
- Handle update case in `exec_module()`
- Add test to test create with `check_mode/action/idempotent`
Hi,

I see there is the easyfix tag & I'm interested by this feature. I'd like to try to do it, if that's OK
@zwindler Welcome your contribution! If you have any problem, please pin me or @zikalino  😄 
Files identified in the description:
* [lib/ansible/modules/cloud/azure/azure_rm_networkinterface.py](https://github.com/ansible/ansible/blob/devel/lib/ansible/modules/cloud/azure/azure_rm_networkinterface.py)

If these files are inaccurate, please update the `component name` section of the description or use the `!component` bot command.

[click here for bot help](https://github.com/ansible/ansibullbot/blob/master/ISSUE_HELP.md)
<!--- boilerplate: components_banner --->
cc @haroldwongms @nitzmahone @trstringer @xscript @yuwzho @zikalino
[click here for bot help](https://github.com/ansible/ansibullbot/blob/master/ISSUE_HELP.md)
<!--- boilerplate: notify --->
+label easyfix

add an easyfix label to attract contributor who is interested in this field. Add a new field need to do the following things:

- Update document at the beginning of the module python file. Including a `version_added: 2.7` line
- Add this to spec in `init()`
- Handle new created case in `exec_module()`
- Handle update case in `exec_module()`
- Add test to test create with `check_mode/action/idempotent`
Hi,

I see there is the easyfix tag & I'm interested by this feature. I'd like to try to do it, if that's OK
@zwindler Welcome your contribution! If you have any problem, please pin me or @zikalino  😄 

Resolving patch:
diff --git a/lib/ansible/modules/cloud/azure/azure_rm_networkinterface.py b/lib/ansible/modules/cloud/azure/azure_rm_networkinterface.py
--- a/lib/ansible/modules/cloud/azure/azure_rm_networkinterface.py
+++ b/lib/ansible/modules/cloud/azure/azure_rm_networkinterface.py
@@ -158,6 +158,12 @@
                 type: bool
                 default: 'no'
         version_added: 2.5
+    enable_accelerated_networking:
+        description:
+            - Specifies whether the network interface should be created with the accelerated networking feature or not
+        type: bool
+        version_added: 2.7
+        default: False
     create_with_security_group:
         description:
             - Specifies whether a default security group should be be created with the NIC. Only applies when creating a new NIC.
@@ -257,6 +263,14 @@
               - name: backendaddrpool1
                 load_balancer: loadbalancer001
 
+    - name: Create a network interface in accelerated networking mode
+      azure_rm_networkinterface:
+        name: nic005
+        resource_group: Testing
+        virtual_network_name: vnet001
+        subnet_name: subnet001
+        enable_accelerated_networking: True
+
     - name: Delete network interface
       azure_rm_networkinterface:
         resource_group: Testing
@@ -364,6 +378,7 @@ def nic_to_dict(nic):
         enable_ip_forwarding=nic.enable_ip_forwarding,
         provisioning_state=nic.provisioning_state,
         etag=nic.etag,
+        enable_accelerated_networking=nic.enable_accelerated_networking,
     )
 
 
@@ -386,6 +401,7 @@ def __init__(self):
             resource_group=dict(type='str', required=True),
             name=dict(type='str', required=True),
             location=dict(type='str'),
+            enable_accelerated_networking=dict(type='bool', default=False),
             create_with_security_group=dict(type='bool', default=True),
             security_group=dict(type='raw', aliases=['security_group_name']),
             state=dict(default='present', choices=['present', 'absent']),
@@ -409,6 +425,7 @@ def __init__(self):
         self.name = None
         self.location = None
         self.create_with_security_group = None
+        self.enable_accelerated_networking = None
         self.security_group = None
         self.private_ip_address = None
         self.private_ip_allocation_method = None
@@ -489,6 +506,12 @@ def exec_module(self, **kwargs):
                     self.log("CHANGED: add or remove network interface {0} network security group".format(self.name))
                     changed = True
 
+                if self.enable_accelerated_networking != bool(results.get('enable_accelerated_networking')):
+                    self.log("CHANGED: Accelerated Networking set to {0} (previously {1})".format(
+                        self.enable_accelerated_networking,
+                        results.get('enable_accelerated_networking')))
+                    changed = True
+
                 if not changed:
                     nsg = self.get_security_group(self.security_group['resource_group'], self.security_group['name'])
                     if nsg and results.get('network_security_group') and results['network_security_group'].get('id') != nsg.id:
@@ -567,6 +590,7 @@ def exec_module(self, **kwargs):
                     location=self.location,
                     tags=self.tags,
                     ip_configurations=nic_ip_configurations,
+                    enable_accelerated_networking=self.enable_accelerated_networking,
                     network_security_group=nsg
                 )
                 self.results['state'] = self.create_or_update_nic(nic)


Test patch:
diff --git a/test/integration/targets/azure_rm_networkinterface/tasks/main.yml b/test/integration/targets/azure_rm_networkinterface/tasks/main.yml
--- a/test/integration/targets/azure_rm_networkinterface/tasks/main.yml
+++ b/test/integration/targets/azure_rm_networkinterface/tasks/main.yml
@@ -252,6 +252,53 @@
       - output.state.ip_configurations[0].public_ip_address == None
       - output.state.network_security_group == None
 
+- name: NIC with Accelerated networking enabled
+  azure_rm_networkinterface:
+      resource_group: "{{ resource_group }}"
+      name: "tn{{ rpfx }}an"
+      virtual_network: "{{ vn.state.id }}"
+      subnet: "tn{{ rpfx }}"
+      enable_accelerated_networking: True
+  register: output
+
+- assert:
+    that:
+      - output.state.enable_accelerated_networking
+      - output.changed
+
+- name: NIC with Accelerated networking enabled (check idempotent)
+  azure_rm_networkinterface:
+      resource_group: "{{ resource_group }}"
+      name: "tn{{ rpfx }}an"
+      virtual_network: "{{ vn.state.id }}"
+      subnet: "tn{{ rpfx }}"
+      enable_accelerated_networking: True
+  register: output
+
+- assert:
+    that:
+      - output.state.enable_accelerated_networking
+      - not output.changed
+
+- name: Disable (previously enabled) Accelerated networking
+  azure_rm_networkinterface:
+      resource_group: "{{ resource_group }}"
+      name: "tn{{ rpfx }}an"
+      virtual_network: "{{ vn.state.id }}"
+      subnet: "tn{{ rpfx }}"
+      enable_accelerated_networking: False
+  register: output
+
+- assert:
+    that:
+      - not output.state.enable_accelerated_networking
+
+- name: Delete AN NIC
+  azure_rm_networkinterface:
+      resource_group: "{{ resource_group }}"
+      name: "tn{{ rpfx }}an"
+      state: absent
+
 - name: Delete the NIC (check mode)
   azure_rm_networkinterface:
       resource_group: "{{ resource_group }}"