Repository: F5Networks/f5-common-python

Issue:
Create 3 missing endpoints at /mgmt/tm/asm/tasks/
Hello Devs!

Greatly enjoy using the SDK. Hopefully these endpoints can be added in the scope of next release :)

```
      "reference": {
        "link": "https://localhost/mgmt/tm/asm/tasks/import-policy"
      }
      "reference": {
        "link": "https://localhost/mgmt/tm/asm/tasks/export-policy"
      }
      "reference": {
        "link": "https://localhost/mgmt/tm/asm/tasks/apply-policy"
      }
```

Discussion:

Patch:
diff --git a/f5/bigip/tm/asm/tasks.py b/f5/bigip/tm/asm/tasks.py
index f669107..2b05f57 100644
--- a/f5/bigip/tm/asm/tasks.py
+++ b/f5/bigip/tm/asm/tasks.py
@@ -91,8 +91,8 @@ class Export_Policy_s(Collection):
 
 class Export_Policy(AsmResource):
     """BIG-IP® ASM Export Policy Resource."""
-    def __init__(self, apply_policy_s):
-        super(Export_Policy, self).__init__(apply_policy_s)
+    def __init__(self, export_policy_s):
+        super(Export_Policy, self).__init__(export_policy_s)
         self._meta_data['required_json_kind'] =\
             'tm:asm:tasks:export-policy:export-policy-taskstate'
         self._meta_data['required_creation_parameters'] = set((
@@ -109,7 +109,33 @@ class Export_Policy(AsmResource):
 
 
 class Import_Policy_s(Collection):
-    pass
+    """BIG-IP® ASM Import Policy Collection."""
+    def __init__(self, tasks):
+        super(Import_Policy_s, self).__init__(tasks)
+        self._meta_data['object_has_stats'] = False
+        self._meta_data['allowed_lazy_attributes'] = [Import_Policy]
+        self._meta_data['attribute_registry'] = {
+            'tm:asm:tasks:import-policy:import-policy-taskstate':
+                Import_Policy}
+
+
+class Import_Policy(AsmResource):
+    """BIG-IP® ASM Import Policy Resource."""
+    def __init__(self, import_policy_s):
+        super(Import_Policy, self).__init__(import_policy_s)
+        self._meta_data['required_json_kind'] =\
+            'tm:asm:tasks:import-policy:import-policy-taskstate'
+        self._meta_data['required_creation_parameters'] = set((
+            'name', 'filename'))
+
+    def modify(self, **kwargs):
+        """Modify is not supported for Apply Policy resource
+
+                :raises: UnsupportedOperation
+        """
+        raise UnsupportedOperation(
+            "%s does not support the modify method" % self.__class__.__name__
+        )
 
 
 class Check_Signatures_s(Collection):


Test patch:
diff --git a/f5/bigip/tm/asm/test/functional/test_tasks.py b/f5/bigip/tm/asm/test/functional/test_tasks.py
index 9496415..3a4d7b8 100644
--- a/f5/bigip/tm/asm/test/functional/test_tasks.py
+++ b/f5/bigip/tm/asm/test/functional/test_tasks.py
@@ -21,6 +21,7 @@ from f5.bigip.tm.asm.tasks import Apply_Policy
 from f5.bigip.tm.asm.tasks import Check_Signature
 from f5.bigip.tm.asm.tasks import Export_Policy
 from f5.bigip.tm.asm.tasks import Export_Signature
+from f5.bigip.tm.asm.tasks import Import_Policy
 from f5.bigip.tm.asm.tasks import Update_Signature
 
 from requests.exceptions import HTTPError
@@ -136,6 +137,23 @@ def set_export_policy_test(request, mgmt_root, reference):
     return exp1
 
 
+def delete_import_policy(mgmt_root):
+    col = mgmt_root.tm.asm.tasks.import_policy_s.get_collection()
+    if len(col) > 0:
+        for i in col:
+            i.delete()
+
+
+def set_import_policy_test(request, mgmt_root, name):
+    def teardown():
+        delete_import_policy(mgmt_root)
+
+    exp1 = mgmt_root.tm.asm.tasks.import_policy_s.import_policy \
+        .create(filename=F, name=name)
+    request.addfinalizer(teardown)
+    return exp1
+
+
 class TestApplyPolicy(object):
     def test_create_req_arg(self, request, mgmt_root, set_policy):
         reference = {'link': set_policy}
@@ -274,6 +292,79 @@ class TestExportPolicy(object):
         assert isinstance(sc[0], Export_Policy)
 
 
+class TestImportPolicy(object):
+    def test_create_req_arg(self, request, mgmt_root):
+        imp1 = set_import_policy_test(request, mgmt_root, 'fake_one')
+        endpoint = str(imp1.id)
+        base_uri = 'https://localhost/mgmt/tm/asm/tasks/import-policy/'
+        final_uri = base_uri+endpoint
+        assert imp1.filename == F
+        assert imp1.selfLink.startswith(final_uri)
+        assert imp1.status == 'NEW'
+        assert imp1.kind == \
+            'tm:asm:tasks:import-policy:import-policy-taskstate'
+        assert imp1.isBase64 is False
+
+    def test_create_optional_args(self, mgmt_root):
+        imp1 = mgmt_root.tm.asm.tasks.import_policy_s.import_policy\
+            .create(filename=F, isBase64=True, name='fake_one')
+        endpoint = str(imp1.id)
+        base_uri = 'https://localhost/mgmt/tm/asm/tasks/import-policy/'
+        final_uri = base_uri+endpoint
+        assert imp1.filename == F
+        assert imp1.selfLink.startswith(final_uri)
+        assert imp1.status == 'NEW'
+        assert imp1.kind == \
+            'tm:asm:tasks:import-policy:import-policy-taskstate'
+        assert imp1.isBase64 is True
+
+    def test_refresh(self, request, mgmt_root):
+        imp1 = set_import_policy_test(request, mgmt_root, 'fake_one')
+        imp2 = mgmt_root.tm.asm.tasks.import_policy_s.import_policy\
+            .load(id=imp1.id)
+        assert imp1.selfLink == imp2.selfLink
+        imp1.refresh()
+        assert imp1.selfLink == imp2.selfLink
+
+    def test_load_no_object(self, mgmt_root):
+        with pytest.raises(HTTPError) as err:
+            mgmt_root.tm.asm.tasks.import_policy_s.import_policy \
+                .load(id='Lx3553-321')
+        assert err.value.response.status_code == 404
+
+    def test_load(self, request, mgmt_root):
+        imp1 = set_import_policy_test(request, mgmt_root, 'fake_one')
+        imp2 = mgmt_root.tm.asm.tasks.import_policy_s.import_policy\
+            .load(id=imp1.id)
+        assert imp1.selfLink == imp2.selfLink
+
+    def test_delete(self, request, mgmt_root):
+        imp1 = set_import_policy_test(request, mgmt_root, 'fake_one')
+        hashid = str(imp1.id)
+        imp1.delete()
+        with pytest.raises(HTTPError) as err:
+            mgmt_root.tm.asm.tasks.import_policy_s.import_policy.load(
+                id=hashid)
+        assert err.value.response.status_code == 404
+
+    def test_policy_import_collection(self, request, mgmt_root):
+        imp1 = set_import_policy_test(request, mgmt_root, 'fake_one')
+        endpoint = str(imp1.id)
+        base_uri = 'https://localhost/mgmt/tm/asm/tasks/import-policy/'
+        final_uri = base_uri+endpoint
+        assert imp1.filename == F
+        assert imp1.selfLink.startswith(final_uri)
+        assert imp1.status == 'NEW'
+        assert imp1.kind == \
+            'tm:asm:tasks:import-policy:import-policy-taskstate'
+        assert imp1.isBase64 is False
+
+        sc = mgmt_root.tm.asm.tasks.import_policy_s.get_collection()
+        assert isinstance(sc, list)
+        assert len(sc)
+        assert isinstance(sc[0], Import_Policy)
+
+
 class TestCheckSignature(object):
     def test_fetch(self, mgmt_root):
         chk1 = mgmt_root.tm.asm.tasks.check_signatures_s.check_signature\
diff --git a/f5/bigip/tm/asm/test/unit/test_tasks.py b/f5/bigip/tm/asm/test/unit/test_tasks.py
index 7c4ec1b..be31d1d 100644
--- a/f5/bigip/tm/asm/test/unit/test_tasks.py
+++ b/f5/bigip/tm/asm/test/unit/test_tasks.py
@@ -19,6 +19,7 @@ from f5.bigip.tm.asm.tasks import Apply_Policy
 from f5.bigip.tm.asm.tasks import Check_Signature
 from f5.bigip.tm.asm.tasks import Export_Policy
 from f5.bigip.tm.asm.tasks import Export_Signature
+from f5.bigip.tm.asm.tasks import Import_Policy
 from f5.bigip.tm.asm.tasks import Update_Signature
 from f5.sdk_exception import MissingRequiredCreationParameter
 from f5.sdk_exception import UnsupportedOperation
@@ -60,6 +61,14 @@ def FakeExportPolicy():
     return fake_exppol
 
 
+@pytest.fixture
+def FakeImportPolicy():
+    fake_asm = mock.MagicMock()
+    fake_imppol = Import_Policy(fake_asm)
+    fake_imppol._meta_data['bigip'].tmos_version = '11.6.0'
+    return fake_imppol
+
+
 @pytest.fixture
 def FakeUpdateSignature():
     fake_asm = mock.MagicMock()
@@ -197,3 +206,29 @@ class TestExportPolicy(object):
         assert kind in list(iterkeys(test_meta))
         assert Export_Policy in test_meta2
         assert t._meta_data['object_has_stats'] is False
+
+
+class TestImportPolicy(object):
+    def test_modify_raises(self, FakeImportPolicy):
+        with pytest.raises(UnsupportedOperation):
+            FakeImportPolicy.modify()
+
+    def test_create_two(self, fakeicontrolsession):
+        b = ManagementRoot('192.168.1.1', 'admin', 'admin')
+        t1 = b.tm.asm.tasks.import_policy_s.import_policy
+        t2 = b.tm.asm.tasks.import_policy_s.import_policy
+        assert t1 is t2
+
+    def test_create_no_args(self, FakeImportPolicy):
+        with pytest.raises(MissingRequiredCreationParameter):
+            FakeImportPolicy.create()
+
+    def test_collection(self, fakeicontrolsession):
+        b = ManagementRoot('192.168.1.1', 'admin', 'admin')
+        t = b.tm.asm.tasks.import_policy_s
+        test_meta = t._meta_data['attribute_registry']
+        test_meta2 = t._meta_data['allowed_lazy_attributes']
+        kind = 'tm:asm:tasks:import-policy:import-policy-taskstate'
+        assert kind in list(iterkeys(test_meta))
+        assert Import_Policy in test_meta2
+        assert t._meta_data['object_has_stats'] is False