github.com/kyma-project/kyma-environment-broker@v0.0.1/testing/e2e/skr/smctl/helpers.js (about)

     1  const execa = require('execa');
     2  const {getEnvOrThrow} = require('../utils');
     3  
     4  async function smInstanceBinding(creds, btpOperatorInstance, btpOperatorBinding) {
     5    let args = [];
     6    try {
     7      args = ['login',
     8        '-a',
     9        creds.url,
    10        '--param',
    11        'subdomain=e2etestingscmigration',
    12        '--auth-flow',
    13        'client-credentials'];
    14      await execa('smctl', args.concat(['--client-id', creds.clientid, '--client-secret', creds.clientsecret]));
    15  
    16      args = ['provision', btpOperatorInstance, 'service-manager', 'service-operator-access', '--mode=sync'];
    17      await execa('smctl', args);
    18  
    19      // Move to Operator Install
    20      args = ['bind', btpOperatorInstance, btpOperatorBinding, '--mode=sync'];
    21      await execa('smctl', args);
    22  
    23      args = ['get-binding', btpOperatorBinding, '-o', 'json'];
    24      const out = await execa('smctl', args);
    25      const b = JSON.parse(out.stdout);
    26      const c = b.items[0].credentials;
    27  
    28      return {
    29        clientId: c.clientid,
    30        clientSecret: c.clientsecret,
    31        smURL: c.sm_url,
    32        url: c.url,
    33        instanceId: b.items[0].service_instance_id,
    34      };
    35    } catch (error) {
    36      if (error.stderr === undefined) {
    37        throw new Error(`failed to process output of "smctl ${args.join(' ')}"`);
    38      }
    39      throw new Error(`failed "smctl ${args.join(' ')}": ${error.stderr}`);
    40    }
    41  }
    42  
    43  async function provisionPlatform(creds, svcatPlatform) {
    44    let args = [];
    45    try {
    46      args = ['login',
    47        '-a',
    48        creds.url,
    49        '--param',
    50        'subdomain=e2etestingscmigration',
    51        '--auth-flow',
    52        'client-credentials'];
    53      await execa('smctl', args.concat(['--client-id', creds.clientid, '--client-secret', creds.clientsecret]));
    54  
    55      // $ smctl register-platform <name> kubernetes -o json
    56      // Output:
    57      // {
    58      //   "id": "<platform-id/cluster-id>",
    59      //   "name": "<name>",
    60      //   "type": "kubernetes",
    61      //   "created_at": "...",
    62      //   "updated_at": "...",
    63      //   "credentials": {
    64      //     "basic": {
    65      //       "username": "...",
    66      //       "password": "..."
    67      //     }
    68      //   },
    69      //   "labels": {
    70      //     "subaccount_id": [
    71      //       "..."
    72      //     ]
    73      //   },
    74      //   "ready": true
    75      // }
    76      args = ['register-platform', svcatPlatform, 'kubernetes', '-o', 'json'];
    77      const registerPlatformOut = await execa('smctl', args);
    78      const platform = JSON.parse(registerPlatformOut.stdout);
    79  
    80      return {
    81        clusterId: platform.id,
    82        name: platform.name,
    83        credentials: platform.credentials.basic,
    84      };
    85    } catch (error) {
    86      if (error.stderr === undefined) {
    87        throw new Error(`failed to process output of "smctl ${args.join(' ')}"`);
    88      }
    89      throw new Error(`failed "smctl ${args.join(' ')}": ${error.stderr}`);
    90    }
    91  }
    92  
    93  async function markForMigration(creds, svcatPlatform, btpOperatorInstanceId) {
    94    let errors = [];
    95    let args = [];
    96    try {
    97      args = ['login',
    98        '-a',
    99        creds.url,
   100        '--param',
   101        'subdomain=e2etestingscmigration',
   102        '--auth-flow',
   103        'client-credentials'];
   104      await execa('smctl', args.concat(['--client-id', creds.clientid, '--client-secret', creds.clientsecret]));
   105    } catch (error) {
   106      errors = errors.concat([`failed "smctl ${args.join(' ')}": ${error.stderr}\n`]);
   107    }
   108  
   109    try {
   110      // usage: smctl curl -X PUT -d '{"sourcePlatformID": ":platformID"}' /v1/migrate/service_operator/:instanceID
   111      const data = {sourcePlatformID: svcatPlatform};
   112      args = ['curl', '-X', 'PUT', '-d', JSON.stringify(data), '/v1/migrate/service_operator/' + btpOperatorInstanceId];
   113      await execa('smctl', args);
   114    } catch (error) {
   115      errors = errors.concat([`failed "smctl ${args.join(' ')}": ${error.stderr}\n`]);
   116    }
   117    if (errors.length > 0) {
   118      throw new Error(errors.join(', '));
   119    }
   120  }
   121  
   122  async function cleanupInstanceBinding(creds, svcatPlatform, btpOperatorInstance, btpOperatorBinding) {
   123    let errors = [];
   124    let args = [];
   125    try {
   126      args = ['login',
   127        '-a',
   128        creds.url,
   129        '--param',
   130        'subdomain=e2etestingscmigration',
   131        '--auth-flow',
   132        'client-credentials'];
   133      await execa('smctl', args.concat(['--client-id', creds.clientid, '--client-secret', creds.clientsecret]));
   134    } catch (error) {
   135      errors = errors.concat([`failed "smctl ${args.join(' ')}": ${error.stderr}\n`]);
   136    }
   137  
   138    try {
   139      args = ['unbind', btpOperatorInstance, btpOperatorBinding, '-f', '--mode=sync'];
   140      const {stdout} = await execa('smctl', args);
   141      if (stdout !== 'Service Binding successfully deleted.') {
   142        errors = errors.concat([`failed "smctl ${args.join(' ')}": ${stdout}`]);
   143      }
   144    } catch (error) {
   145      errors = errors.concat([`failed "smctl ${args.join(' ')}": ${error.stderr}\n${error}`]);
   146    }
   147  
   148    try {
   149      // hint: probably should fail cause that instance created other instances (after the migration is done)
   150      args = ['deprovision', btpOperatorInstance, '-f', '--mode=sync'];
   151      const {stdout} = await execa('smctl', args);
   152      if (stdout !== 'Service Instance successfully deleted.') {
   153        errors = errors.concat([`failed "smctl ${args.join(' ')}": ${stdout}`]);
   154      }
   155    } catch (error) {
   156      errors = errors.concat([`failed "smctl ${args.join(' ')}": ${error.stderr}\n${error}`]);
   157    }
   158  
   159    try {
   160      args = ['delete-platform', svcatPlatform, '-f', '--cascade'];
   161      await execa('smctl', args);
   162      // if (stdout !== "Platform(s) successfully deleted.") {
   163      //     errors = errors.concat([`failed "smctl ${args.join(' ')}": ${stdout}`])
   164      // }
   165    } catch (error) {
   166      errors = errors.concat([`failed "smctl ${args.join(' ')}": ${error.stderr}\n`]);
   167    }
   168  
   169    if (errors.length > 0) {
   170      throw new Error(errors.join(', '));
   171    }
   172  }
   173  
   174  
   175  class AdminCreds {
   176    static fromEnv() {
   177      return new AdminCreds(
   178          getEnvOrThrow('BTP_SM_ADMIN_CLIENTID'),
   179          getEnvOrThrow('BTP_SM_ADMIN_CLIENTSECRET'),
   180          getEnvOrThrow('BTP_SM_ADMIN_URL'),
   181      );
   182    }
   183  
   184    constructor(clientid, clientsecret, url) {
   185      this.clientid = clientid;
   186      this.clientsecret = clientsecret;
   187      this.url = url;
   188    }
   189  }
   190  
   191  class BTPOperatorCreds {
   192    static fromEnv() {
   193      return new BTPOperatorCreds(
   194          getEnvOrThrow('BTP_OPERATOR_CLIENTID'),
   195          getEnvOrThrow('BTP_OPERATOR_CLIENTSECRET'),
   196          getEnvOrThrow('BTP_OPERATOR_URL'),
   197          getEnvOrThrow('BTP_OPERATOR_TOKENURL'),
   198      );
   199    }
   200  
   201    static dummy() {
   202      return new BTPOperatorCreds(
   203          'dummy_client_id',
   204          'dummy_client_secret',
   205          'dummy_url',
   206          'dummy_token_url',
   207      );
   208    }
   209  
   210    constructor(clientid, clientsecret, smURL, url) {
   211      this.clientid = clientid;
   212      this.clientsecret = clientsecret;
   213      this.smURL = smURL;
   214      this.url = url;
   215    }
   216  }
   217  
   218  module.exports = {
   219    provisionPlatform,
   220    smInstanceBinding,
   221    markForMigration,
   222    cleanupInstanceBinding,
   223    AdminCreds: AdminCreds,
   224    BTPOperatorCreds: BTPOperatorCreds,
   225  };