Repository: getmoto/moto

Issue:
[iam] create_policy_version: VersionId calculation is incorrect
`create_policy_version` uses the length of the versions attribute of the `Policy` being updated to determine the next version id. This is incorrect if some previous version has been deleted by calling `delete_policy_version`. 

```
@mock_iam
def test_policy_version():
    conn = boto3.client("iam")

    # Create policy version v1
    policy = conn.create_policy(PolicyName="policy", PolicyDocument="A")
    arn = policy["Policy"]["Arn"]

    # Create policy version v2
    version = conn.create_policy_version(PolicyArn=arn, PolicyDocument="B", SetAsDefault=True)
    version_id = version["PolicyVersion"]["VersionId"]
    assert version_id == "v2"

    # Delete policy version v1
    conn.delete_policy_version(PolicyArn=arn, VersionId="v1")

    # Create another policy version. This should be version v3, not v2. Version v2 is taken!
    version = conn.create_policy_version(PolicyArn=arn, PolicyDocument="C")
    version_id = version["PolicyVersion"]["VersionId"]

    # There are now two versions with version id 2. Getting v2 incorrectly fetches the older one
    document = conn.get_policy_version(PolicyArn=arn, VersionId="v2")["PolicyVersion"]["Document"]
    assert document == "C", "Got: {}".format(document)  # we get B instead


Discussion:

Resolving patch:
diff --git a/moto/iam/models.py b/moto/iam/models.py
--- a/moto/iam/models.py
+++ b/moto/iam/models.py
@@ -48,7 +48,13 @@ def __init__(self,
         self.description = description or ''
         self.id = random_policy_id()
         self.path = path or '/'
-        self.default_version_id = default_version_id or 'v1'
+
+        if default_version_id:
+            self.default_version_id = default_version_id
+            self.next_version_num = int(default_version_id.lstrip('v')) + 1
+        else:
+            self.default_version_id = 'v1'
+            self.next_version_num = 2
         self.versions = [PolicyVersion(self.arn, document, True)]
 
         self.create_datetime = datetime.now(pytz.utc)
@@ -716,7 +722,8 @@ def create_policy_version(self, policy_arn, policy_document, set_as_default):
             raise IAMNotFoundException("Policy not found")
         version = PolicyVersion(policy_arn, policy_document, set_as_default)
         policy.versions.append(version)
-        version.version_id = 'v{0}'.format(len(policy.versions))
+        version.version_id = 'v{0}'.format(policy.next_version_num)
+        policy.next_version_num += 1
         if set_as_default:
             policy.default_version_id = version.version_id
         return version


Test patch:
diff --git a/tests/test_iam/test_iam.py b/tests/test_iam/test_iam.py
--- a/tests/test_iam/test_iam.py
+++ b/tests/test_iam/test_iam.py
@@ -303,8 +303,17 @@ def test_create_policy_versions():
         PolicyDocument='{"some":"policy"}')
     version = conn.create_policy_version(
         PolicyArn="arn:aws:iam::123456789012:policy/TestCreatePolicyVersion",
-        PolicyDocument='{"some":"policy"}')
+        PolicyDocument='{"some":"policy"}',
+        SetAsDefault=True)
     version.get('PolicyVersion').get('Document').should.equal({'some': 'policy'})
+    version.get('PolicyVersion').get('VersionId').should.equal("v2")
+    conn.delete_policy_version(
+        PolicyArn="arn:aws:iam::123456789012:policy/TestCreatePolicyVersion",
+        VersionId="v1")
+    version = conn.create_policy_version(
+        PolicyArn="arn:aws:iam::123456789012:policy/TestCreatePolicyVersion",
+        PolicyDocument='{"some":"policy"}')
+    version.get('PolicyVersion').get('VersionId').should.equal("v3")
 
 
 @mock_iam