Repository: opsgenie/opsgenie-cloudformation-resources (language: python)

Issue:
Test versioning



Discussion / hints:

Fix patch:
diff --git a/Dockerfile b/Dockerfile
new file mode 100644
index 0000000..517a379
--- /dev/null
+++ b/Dockerfile
@@ -0,0 +1,16 @@
+FROM maven:3.8-jdk-8
+
+COPY requirements.txt .
+
+RUN apt-get update
+RUN apt-get install -y python3
+RUN apt-get install -y python3-pip
+RUN pip3 install pip --upgrade
+RUN pip3 install -r requirements.txt
+RUN curl --silent --show-error --fail "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip"
+RUN  unzip awscliv2.zip
+RUN  ./aws/install
+RUN  apt install -y groff
+
+ENTRYPOINT ["tail"]
+CMD ["-f","/dev/null"]
\ No newline at end of file
diff --git a/bitbucket-pipelines.yml b/bitbucket-pipelines.yml
new file mode 100644
index 0000000..f3b86db
--- /dev/null
+++ b/bitbucket-pipelines.yml
@@ -0,0 +1,158 @@
+clone:
+  depth: 10
+
+image:
+  name: docker-public.packages.atlassian.com/atlassian/bitbucket-pipelines-mvn-python3-awscli
+
+
+pipelines:
+  custom:
+    Publish-Integration-Resource:
+      - variables:
+        - name: REGION
+
+      - step:
+          name: Build and Register Resource
+          oidc: true
+          script:
+            - cd opsgenie_integration
+            - export AWS_REGION=$REGION
+            - export AWS_ROLE_ARN=${ROLE_ARN}
+            - export AWS_WEB_IDENTITY_TOKEN_FILE=$(pwd)/web-identity-token
+            - export RESOURCE_NAME=Atlassian::Opsgenie::Integration
+            - export RESOURCE_PACKAGE=atlassian-opsgenie-integration
+            - echo $BITBUCKET_STEP_OIDC_TOKEN > $(pwd)/web-identity-token
+            - ../script.sh
+            - ../register-resource.sh
+
+      - step:
+          name: Run Contract Test
+          oidc: true
+          script:
+            - cd opsgenie_integration
+            - export AWS_REGION=$REGION
+            - export AWS_ROLE_ARN=${ROLE_ARN}
+            - export AWS_WEB_IDENTITY_TOKEN_FILE=$(pwd)/web-identity-token
+            - export RESOURCE_NAME=Atlassian::Opsgenie::Integration
+            - echo $BITBUCKET_STEP_OIDC_TOKEN > $(pwd)/web-identity-token
+            - ../test-check.sh
+
+      - step:
+          name: Publish Resource
+          oidc: true
+          trigger: manual
+          script:
+            - cd opsgenie_integration
+            - export AWS_REGION=$REGION
+            - export AWS_ROLE_ARN=${ROLE_ARN}
+            - export AWS_WEB_IDENTITY_TOKEN_FILE=$(pwd)/web-identity-token
+            - echo $BITBUCKET_STEP_OIDC_TOKEN > $(pwd)/web-identity-token
+            - export RESOURCE_NAME=Atlassian::Opsgenie::Integration
+            - VERSION=$(mvn -q -Dexec.executable=echo -Dexec.args='${project.version}' --non-recursive exec:exec)
+            - aws cloudformation publish-type --type RESOURCE --type-name $RESOURCE_NAME  --public-version-number $VERSION
+
+
+    Publish-Team-Resource:
+      - variables:
+        - name: REGION
+      - step:
+          name: Build and Register Resource
+          oidc: true
+          script:
+            - cd opsgenie_team
+            - export AWS_REGION=$REGION
+            - export AWS_ROLE_ARN=${ROLE_ARN}
+            - export AWS_WEB_IDENTITY_TOKEN_FILE=$(pwd)/web-identity-token
+            - export RESOURCE_NAME=Atlassian::Opsgenie::Team
+            - export RESOURCE_PACKAGE=atlassian-opsgenie-team
+            - echo $BITBUCKET_STEP_OIDC_TOKEN > $(pwd)/web-identity-token
+            - ../script.sh
+            - ../register-resource.sh
+
+      - step:
+          name: Run Contract Test
+          oidc: true
+          script:
+            - cd opsgenie_team
+            - export AWS_REGION=$REGION
+            - export AWS_ROLE_ARN=${ROLE_ARN}
+            - export AWS_WEB_IDENTITY_TOKEN_FILE=$(pwd)/web-identity-token
+            - export RESOURCE_NAME=Atlassian::Opsgenie::Team
+            - echo $BITBUCKET_STEP_OIDC_TOKEN > $(pwd)/web-identity-token
+            - ../test-check.sh
+      - step:
+          name: Publish Resource
+          oidc: true
+          trigger: manual
+          script:
+            - cd opsgenie_integration
+            - export AWS_REGION=$REGION
+            - export AWS_ROLE_ARN=${ROLE_ARN}
+            - export AWS_WEB_IDENTITY_TOKEN_FILE=$(pwd)/web-identity-token
+            - export RESOURCE_NAME=Atlassian::Opsgenie::Team
+            - echo $BITBUCKET_STEP_OIDC_TOKEN > $(pwd)/web-identity-token
+            - VERSION=$(mvn -q -Dexec.executable=echo -Dexec.args='${project.version}' --non-recursive exec:exec)
+            - aws cloudformation publish-type --type RESOURCE --type-name $RESOURCE_NAME --public-version-number $VERSION
+
+
+    Publish-User-Resource:
+      - variables:
+        - name: REGION
+      - step:
+          name: Build and Register Resource
+          oidc: true
+          script:
+            - cd opsgenie_user
+            - export AWS_REGION=$REGION
+            - export AWS_ROLE_ARN=${ROLE_ARN}
+            - export AWS_WEB_IDENTITY_TOKEN_FILE=$(pwd)/web-identity-token
+            - export RESOURCE_NAME=Atlassian::Opsgenie::User
+            - export RESOURCE_PACKAGE=atlassian-opsgenie-user
+            - echo $BITBUCKET_STEP_OIDC_TOKEN > $(pwd)/web-identity-token
+            - ../script.sh
+            - ../register-resource.sh
+
+      - step:
+          name: Run Contract Test
+          oidc: true
+          script:
+            - cd opsgenie_user
+            - export AWS_REGION=$REGION
+            - export AWS_ROLE_ARN=${ROLE_ARN}
+            - export AWS_WEB_IDENTITY_TOKEN_FILE=$(pwd)/web-identity-token
+            - export RESOURCE_NAME=Atlassian::Opsgenie::User
+            - echo $BITBUCKET_STEP_OIDC_TOKEN > $(pwd)/web-identity-token
+            - ../test-check.sh
+
+      - step:
+          name: Publish Resource
+          oidc: true
+          trigger: manual
+          script:
+            - cd opsgenie_integration
+            - export AWS_REGION=$REGION
+            - export AWS_ROLE_ARN=${ROLE_ARN}
+            - export AWS_WEB_IDENTITY_TOKEN_FILE=$(pwd)/web-identity-token
+            - export RESOURCE_NAME=Atlassian::Opsgenie::User
+            - echo $BITBUCKET_STEP_OIDC_TOKEN > $(pwd)/web-identity-token
+            - VERSION=$(mvn -q -Dexec.executable=echo -Dexec.args='${project.version}' --non-recursive exec:exec)
+            - aws cloudformation publish-type --type RESOURCE --type-name $RESOURCE_NAME --public-version-number $VERSION
+
+
+    Register-Publisher:
+      - variables:
+        - name: REGION
+      - step:
+          name: Build Resource and Register Resource
+          oidc: true
+          script:
+            - export AWS_REGION=$REGION
+            - export AWS_ROLE_ARN=${ROLE_ARN}
+            - export AWS_WEB_IDENTITY_TOKEN_FILE=$(pwd)/web-identity-token
+            - echo $BITBUCKET_STEP_OIDC_TOKEN > $(pwd)/web-identity-token
+            - aws cloudformation register-publisher --connection-arn $CONNECTION_ARN --accept-terms-and-conditions
+  pull-requests:
+    master:
+      - step:
+          Build Check:
+            - mvn package
\ No newline at end of file
diff --git a/opsgenie_integration/pom.xml b/opsgenie_integration/pom.xml
index 992cd3a..6be609c 100644
--- a/opsgenie_integration/pom.xml
+++ b/opsgenie_integration/pom.xml
@@ -30,6 +30,13 @@
             <url>http://central.maven.org/maven2</url>
         </repository>
     </repositories>
+    <dependencies>
+        <dependency>
+            <groupId>software.amazon.cloudformation</groupId>
+            <artifactId>aws-cloudformation-rpdk-java-plugin</artifactId>
+            <version>2.0.3</version>
+        </dependency>
+    </dependencies>
     <build>
         <plugins>
             <plugin>
diff --git a/opsgenie_team/pom.xml b/opsgenie_team/pom.xml
index 0af0c31..75c9324 100644
--- a/opsgenie_team/pom.xml
+++ b/opsgenie_team/pom.xml
@@ -25,6 +25,14 @@
         <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
     </properties>
 
+    <dependencies>
+        <dependency>
+            <groupId>software.amazon.cloudformation</groupId>
+            <artifactId>aws-cloudformation-rpdk-java-plugin</artifactId>
+            <version>2.0.3</version>
+        </dependency>
+    </dependencies>
+
     <repositories>
         <repository>
             <id>central</id>
diff --git a/opsgenie_user/atlassian-opsgenie-user.json b/opsgenie_user/atlassian-opsgenie-user.json
index 472788f..43048d2 100644
--- a/opsgenie_user/atlassian-opsgenie-user.json
+++ b/opsgenie_user/atlassian-opsgenie-user.json
@@ -45,6 +45,10 @@
   "readOnlyProperties": [
     "/properties/UserId"
   ],
+  "createOnlyProperties": [
+    "/properties/Username",
+    "/properties/FullName"
+  ],
   "primaryIdentifier": [
     "/properties/UserId"
   ],
diff --git a/opsgenie_user/overrides.json b/opsgenie_user/overrides.json
index 0b4ce78..45cbd17 100644
--- a/opsgenie_user/overrides.json
+++ b/opsgenie_user/overrides.json
@@ -1,9 +1,17 @@
 {
-  "CREATE": {
-    "/OpsgenieApiKey": "",
-    "/OpsgenieApiEndpoint": "",
-    "/Role": "User",
-    "/FullName": "test user123",
-    "/UserId": ""
-  }
+    "CREATE": {
+        "/OpsgenieApiEndpoint": "",
+        "/FullName": "test user123",
+        "/Username": "test@test.com",
+        "/OpsgenieApiKey": "",
+        "/Role": "User"
+    },
+    "UPDATE": {
+        "/OpsgenieApiEndpoint": "",
+        "/FullName": "test user123",
+        "/Username": "test@test.com",
+        "/OpsgenieApiKey": "",
+        "/Role": "User"
+    }
+
 }
\ No newline at end of file
diff --git a/opsgenie_user/pom.xml b/opsgenie_user/pom.xml
index ba5e7a0..632d35d 100644
--- a/opsgenie_user/pom.xml
+++ b/opsgenie_user/pom.xml
@@ -24,6 +24,14 @@
         <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
     </properties>
 
+    <dependencies>
+        <dependency>
+            <groupId>software.amazon.cloudformation</groupId>
+            <artifactId>aws-cloudformation-rpdk-java-plugin</artifactId>
+            <version>2.0.3</version>
+        </dependency>
+    </dependencies>
+
     <repositories>
         <repository>
             <id>central</id>
@@ -64,7 +72,7 @@
                 <groupId>org.codehaus.mojo</groupId>
                 <artifactId>exec-maven-plugin</artifactId>
                 <version>1.6.0</version>
-               <executions>
+                <executions>
                     <execution>
                         <id>generate</id>
                         <phase>generate-sources</phase>
diff --git a/opsgenie_user/src/main/java/com/atlassian/opsgenie/user/ListHandler.java b/opsgenie_user/src/main/java/com/atlassian/opsgenie/user/ListHandler.java
index a3d4b6b..66eeff1 100644
--- a/opsgenie_user/src/main/java/com/atlassian/opsgenie/user/ListHandler.java
+++ b/opsgenie_user/src/main/java/com/atlassian/opsgenie/user/ListHandler.java
@@ -6,7 +6,7 @@
 import com.atlassian.opsgenie.user.model.DataModel;
 import com.atlassian.opsgenie.user.model.ListUserResponse;
 
-import javax.annotation.Resource;
+//import javax.annotation.Resource;
 import java.io.IOException;
 import java.util.ArrayList;
 import java.util.List;
diff --git a/register-resource.sh b/register-resource.sh
new file mode 100755
index 0000000..41ac4b8
--- /dev/null
+++ b/register-resource.sh
@@ -0,0 +1,51 @@
+#
+#!/bin/bash
+
+function readJson() {
+  UNAMESTR=$(uname)
+  if [[ "$UNAMESTR" == 'Linux' ]]; then
+    SED_EXTENDED='-r'
+  elif [[ "$UNAMESTR" == 'Darwin' ]]; then
+    SED_EXTENDED='-E'
+  fi
+
+  VALUE=$(grep -m 1 "\"${2}\"" ${1} | sed ${SED_EXTENDED} 's/^ *//;s/.*: *"//;s/",?//')
+
+  if [ ! "$VALUE" ]; then
+    echo "Error: Cannot find \"${2}\" in ${1}" >&2
+    exit 1
+  else
+    echo $VALUE
+  fi
+}
+
+
+mvn package
+if [[ "$?" -ne 0 ]] ; then
+  echo 'build failed'; exit 1
+fi
+
+cfn submit --dry-run
+aws s3 cp $RESOURCE_PACKAGE.zip  s3://$BUCKET_NAME
+aws cloudformation register-type --type RESOURCE --type-name $RESOURCE_NAME --schema-handler-package s3://$BUCKET_NAME/$RESOURCE_PACKAGE.zip > registration_token.json
+
+REGISTRATION_TOKEN=$(readJson registration_token.json RegistrationToken) || exit 1
+
+aws cloudformation describe-type-registration --registration-token $REGISTRATION_TOKEN >registration_status.json
+
+REGISTRATION_STATUS=$(readJson registration_status.json ProgressStatus) || exit 1
+
+while [ $REGISTRATION_STATUS = "IN_PROGRESS" ]; do
+  aws cloudformation describe-type-registration --registration-token $REGISTRATION_TOKEN >registration_status.json
+  REGISTRATION_STATUS=$(readJson registration_status.json ProgressStatus) || exit 1
+done
+
+echo $REGISTRATION_STATUS
+
+if [ $REGISTRATION_STATUS = "COMPLETE" ]; then
+  TYPE_VERSION_ARN=$(readJson registration_status.json TypeVersionArn) || exit 1
+  echo $TYPE_VERSION_ARN
+  aws cloudformation set-type-default-version --arn $TYPE_VERSION_ARN
+else
+  exit 1
+fi
diff --git a/requirements.txt b/requirements.txt
new file mode 100644
index 0000000..f01b1c0
--- /dev/null
+++ b/requirements.txt
@@ -0,0 +1,5 @@
+cloudformation-cli==0.2.12
+cloudformation-cli-go-plugin==2.0.4
+cloudformation-cli-java-plugin==2.0.5
+cloudformation-cli-python-plugin==2.1.3
+cloudformation-cli-typescript-plugin==1.0.0
\ No newline at end of file
diff --git a/script.sh b/script.sh
new file mode 100755
index 0000000..6a30d70
--- /dev/null
+++ b/script.sh
@@ -0,0 +1,25 @@
+#
+#!/bin/bash
+
+read -r -d '' PYSCRIPT << ENDPY
+import json
+import os
+def updateJsonFile():
+
+    with open('overrides.json', 'r') as f:
+        data = json.load(f)
+        data["CREATE"]["/OpsgenieApiKey"]= os.environ['OPSGENIE_API_KEY']
+        data["CREATE"]["/OpsgenieApiEndpoint"]= os.environ['OPSGENIE_API_ENDPOINT']
+        if hasattr( data ,"UPDATE"):
+          data["UPDATE"]["/OpsgenieApiKey"]= os.environ['OPSGENIE_API_KEY']
+          data["UPDATE"]["/OpsgenieApiEndpoint"]= os.environ['OPSGENIE_API_ENDPOINT']
+        print(data);
+        with open('overrides.json', 'w') as f1:
+            json.dump(data, f1, indent=4)
+
+updateJsonFile()
+ENDPY
+
+# execute python and fail on error
+
+echo "$PYSCRIPT" |python - || exit 1


Test patch:
diff --git a/test-check.sh b/test-check.sh
new file mode 100755
index 0000000..c291b9e
--- /dev/null
+++ b/test-check.sh
@@ -0,0 +1,47 @@
+#
+#!/bin/bash
+
+function readJson {
+  UNAMESTR=`uname`
+  if [[ "$UNAMESTR" == 'Linux' ]]; then
+    SED_EXTENDED='-r'
+  elif [[ "$UNAMESTR" == 'Darwin' ]]; then
+    SED_EXTENDED='-E'
+  fi;
+
+  VALUE=`grep -m 1 "\"${2}\"" ${1} | sed ${SED_EXTENDED} 's/^ *//;s/.*: *"//;s/",?//'`
+
+  if [ ! "$VALUE" ]; then
+    echo "Error: Cannot find \"${2}\" in ${1}" >&2;
+    exit 1;
+  else
+    echo $VALUE ;
+  fi;
+}
+
+aws cloudformation test-type --type RESOURCE --type-name $RESOURCE_NAME --log-delivery-bucket $BUCKET_NAME> test_arn.json
+
+TEST_ARN=`readJson test_arn.json TypeVersionArn` || exit 1;
+
+echo $TEST_ARN
+
+aws cloudformation describe-type --arn $TEST_ARN > test-result.json
+
+TEST_STATUS=`readJson test-result.json TypeTestsStatus` || exit 1;
+
+while [ $TEST_STATUS = "IN_PROGRESS" ]; do
+  sleep 60
+  aws cloudformation describe-type --arn $TEST_ARN > test-result.json
+  TEST_STATUS=`readJson test-result.json TypeTestsStatus` || exit 1;
+  echo $TEST_STATUS
+done
+
+if [ $TEST_STATUS = "PASSED" ]; then
+  echo $TEST_STATUS
+  echo $TEST_STATUS > test_result.txt
+else
+  echo $TEST_STATUS
+  echo $TEST_STATUS > test_result.txt
+  exit 1
+fi
+