link: function(scope, elem) {
   AccountService.listProviders().then(function(providers) {
     if (!providers || !providers.length || providers.length === 1) {
       elem.show();
     } else {
       elem.hide();
     }
   });
 },
 function normalizeLoadBalancer(loadBalancer) {
   return AccountService.getAccountDetails(loadBalancer.account).then(accountDetails => {
     return providerServiceDelegate
       .getDelegate(
         loadBalancer.provider || loadBalancer.type,
         'loadBalancer.transformer',
         accountDetails && accountDetails.skin,
       )
       .normalizeLoadBalancer(loadBalancer);
   });
 }
 function normalizeServerGroup(serverGroup, application) {
   const account = serverGroup.account;
   if (account) {
     return AccountService.getAccountDetails(account).then(accountDetails => {
       // If there is a versioned cloud provider, and the user does not have permission to view the account itself, it will
       // fail to get the accountDetails and thus fail to get the appropriate skin.
       return normalizeServerGroupForSkin(serverGroup, application, accountDetails && accountDetails.skin);
     });
   } else {
     return $q.resolve(normalizeServerGroupForSkin(serverGroup, application));
   }
 }
 let initialize = () => {
   if (this.verification.toVerify) {
     this.required = true;
     this.verification.verified = false;
   }
   if (this.account) {
     this.verification.toVerify = this.account;
     AccountService.challengeDestructiveActions(this.account).then(challenge => {
       this.required = challenge;
       this.verification.verified = !challenge;
     });
   }
 };
 function getSnapshotEnabledAccounts(application) {
   return AccountService.listProviders(application)
     .then(providers =>
       providers.filter(provider => CloudProviderRegistry.getValue(provider, 'snapshotsEnabled')),
     )
     .then(snapshotEnabledProviders =>
       $q.all(snapshotEnabledProviders.map(provider => AccountService.listAccounts(provider))),
     )
     .then(accounts =>
       _.chain(accounts)
         .flatten()
         .map('name')
         .value(),
     );
 }
 link: function(scope) {
   scope.initialized = false;
   var getProviderList = scope.providers ? $q.when(scope.providers.sort()) : AccountService.listProviders();
   getProviderList.then(function(providers) {
     scope.initialized = true;
     if (!providers.length) {
       scope.component[scope.field] = 'aws';
     }
     if (providers.length === 1) {
       scope.component[scope.field] = providers[0];
     }
     if (providers.length > 1) {
       scope.providers = providers;
       scope.showSelector = true;
     }
   });
 },
    function($scope, $q, $log, $state, $uibModalInstance, $timeout) {
      let applicationLoader = ApplicationReader.listApplications();
      applicationLoader.then(applications => (this.data.appNameList = _.map(applications, 'name')));

      let providerLoader = AccountService.listProviders();
      providerLoader.then(providers => (this.data.cloudProviders = providers));

      $q.all([applicationLoader, providerLoader])
        .catch(error => {
          this.state.initializeFailed = true;
          throw error;
        })
        .finally(() => (this.state.initializing = false));

      this.state = {
        initializing: true,
        initializeFailed: false,
        submitting: false,
        errorMessages: [],
        permissionsInvalid: false,
      };
      this.data = {
        gitSources: SETTINGS.gitSources || ['stash', 'github', 'bitbucket', 'gitlab'],
      };
      this.application = {
        cloudProviders: [],
        instancePort: SETTINGS.defaultInstancePort || null,
      };

      let submitting = () => {
        this.state.errorMessages = [];
        this.state.submitting = true;
      };

      let goIdle = () => {
        this.state.submitting = false;
      };

      var navigateTimeout = null;

      let routeToApplication = () => {
        navigateTimeout = $timeout(() => {
          $state.go('home.applications.application.insight.clusters', {
            application: this.application.name,
          });
        }, 1000);
      };

      $scope.$on('$destroy', () => $timeout.cancel(navigateTimeout));

      let waitUntilApplicationIsCreated = task => {
        return TaskReader.waitUntilTaskCompletes(task).then(routeToApplication, () => {
          this.state.errorMessages.push('Could not create application: ' + task.failureMessage);
          goIdle();
        });
      };

      let createApplicationFailure = () => {
        this.state.errorMessages.push('Could not create application');
        goIdle();
      };

      this.createApplicationForTests = () => {
        return ApplicationWriter.createApplication(this.application).then(
          waitUntilApplicationIsCreated,
          createApplicationFailure,
        );
      };

      this.updateCloudProviderHealthWarning = () => {
        if (!this.application.platformHealthOnlyShowOverride) {
          // Show the warning if platformHealthOnlyShowOverride is being enabled.
          this.data.showOverrideWarning = `Simply enabling the "Consider only cloud provider health when executing tasks"
          option above is usually sufficient for most applications that want the same health provider behavior for
          all stages. Note that pipelines will require manual updating if this setting is disabled in the future.`;
        }
      };

      function permissionsAreValid(permissions) {
        if (permissions.READ.includes(null) || permissions.WRITE.includes(null)) {
          return false;
        }
        if (permissions.READ.length > 0 && permissions.WRITE.length === 0) {
          return false;
        }
        return true;
      }

      this.handlePermissionsChange = permissions => {
        this.state.permissionsInvalid = !permissionsAreValid(permissions);
        this.application.permissions = permissions;
        $scope.$digest();
      };

      this.submit = () => {
        submitting();
        this.application.name = this.application.name.toLowerCase();
        if (this.data.cloudProviders.length === 1) {
          this.application.cloudProviders = this.data.cloudProviders;
        }
        this.createApplicationForTests();
      };
    },
 $q.all(snapshotEnabledProviders.map(provider => AccountService.listAccounts(provider))),