github.com/ouraigua/jenkins-library@v0.0.0-20231028010029-fbeaf2f3aa9b/src/com/sap/piper/integration/CloudFoundry.groovy (about)

     1  package com.sap.piper.integration
     2  
     3  class CloudFoundry {
     4      final Script script
     5  
     6      CloudFoundry(Script script) {
     7          this.script = script
     8      }
     9  
    10      def getXsuaaCredentials(String apiEndpoint, String org, String space, String credentialsId, String appName, boolean verbose){
    11          def env = getAppEnvironment(apiEndpoint, org, space, credentialsId, appName, verbose)
    12          if (!env.system_env_json.VCAP_SERVICES.xsuaa[0]) throw new Exception("Cannot find xsuaa credentials")
    13          return env.system_env_json.VCAP_SERVICES.xsuaa[0].credentials
    14      }
    15  
    16      def getAppEnvironment(String apiEndpoint, String org, String space, String credentialsId, String appName, boolean verbose){
    17          def authEndpoint = getAuthEndPoint(apiEndpoint, verbose)
    18          def bearerToken = getBearerToken(authEndpoint, credentialsId, verbose)
    19          def appUrl = getAppRefUrl(apiEndpoint, org, space, bearerToken, appName, verbose)
    20          def response = script.httpRequest url: "${appUrl}/env", quiet: !verbose,
    21                          customHeaders:[[name: 'Authorization', value: "${bearerToken}"]]
    22          def envJson = script.readJSON text:"${response.content}"
    23          return envJson
    24      }
    25  
    26      def getAuthEndPoint(String apiEndpoint, boolean verbose){
    27          def info = script.httpRequest url: "${apiEndpoint}/v2/info", quiet: !verbose
    28          def responseJson = script.readJSON text:"${info.content}"
    29          return responseJson.authorization_endpoint
    30      }
    31  
    32      def getBearerToken(String authorizationEndpoint, String credentialsId, boolean verbose){
    33          script.withCredentials([script.usernamePassword(credentialsId: credentialsId, usernameVariable: 'usercf', passwordVariable: 'passwordcf')]) {
    34              def token = script.httpRequest url:"${authorizationEndpoint}/oauth/token", quiet: !verbose,
    35                                  httpMode:'POST',
    36                                  requestBody: "username=${script.usercf}&password=${script.passwordcf}&client_id=cf&grant_type=password&response_type=token",
    37                                  customHeaders: [[name: 'Content-Type', value: 'application/x-www-form-urlencoded'],[name: 'Authorization', value: 'Basic Y2Y6']]
    38              def responseJson = script.readJSON text:"${token.content}"
    39              return "Bearer ${responseJson.access_token.trim()}"
    40          }
    41      }
    42  
    43      def getAppRefUrl(String apiEndpoint, String org, String space, String bearerToken, String appName, boolean verbose){
    44          def orgGuid = getOrgGuid(apiEndpoint, org, bearerToken, verbose)
    45          def spaceGuid = getSpaceGuid(apiEndpoint, orgGuid, space, bearerToken, verbose)
    46          def appInfo = script.httpRequest  url:"${apiEndpoint}/v3/apps?names=${appName},${appName}_blue,${appName}_green,${appName}-blue,${appName}-green&space_guids=${spaceGuid}",  quiet: !verbose,
    47                              customHeaders:[[name: 'Authorization', value: "${bearerToken}"]]
    48          def responseJson = script.readJSON text:"${appInfo.content}"
    49          if (!responseJson.resources[0]) throw new Exception("Cannot find Application")
    50          return responseJson.resources[0].links.self.href.trim()
    51      }
    52  
    53      def getOrgGuid(String apiEndpoint, String org, String bearerToken, boolean verbose){
    54          def organizationInfo = script.httpRequest url: "${apiEndpoint}/v3/organizations?names=${org}", quiet: !verbose,
    55                                      customHeaders:[[name: 'Authorization', value: "${bearerToken}"]]
    56          def responseJson = script.readJSON text:"${organizationInfo.content}"
    57          if (!responseJson.resources[0]) throw new Exception("Cannot find Organization")
    58          return responseJson.resources[0].guid
    59      }
    60  
    61      def getSpaceGuid(String apiEndpoint, String orgGuid, String space, String bearerToken, boolean verbose){
    62          def spaceInfo = script.httpRequest url: "${apiEndpoint}/v3/spaces?names=${space}&organization_guids=${orgGuid}", quiet: !verbose,
    63                                  customHeaders:[[name: 'Authorization', value: "${bearerToken}"]]
    64          def responseJson = script.readJSON text:"${spaceInfo.content}"
    65          if (!responseJson.resources[0]) throw new Exception("Cannot find Space")
    66          return responseJson.resources[0].guid
    67      }
    68  }