<commit_before>'use strict';

import * as angular from 'angular';
import _ from 'lodash';

import { AccountService, FirewallLabels } from '@spinnaker/core';
import { AMAZON_SECURITYGROUP_CONFIGURE_CONFIGSECURITYGROUP_MIXIN_CONTROLLER } from '../configure/configSecurityGroup.mixin.controller';

export const AMAZON_SECURITYGROUP_CLONE_CLONESECURITYGROUP_CONTROLLER =
  'spinnaker.amazon.securityGroup.clone.controller';
export const name = AMAZON_SECURITYGROUP_CLONE_CLONESECURITYGROUP_CONTROLLER; // for backwards compatibility
angular
  .module(AMAZON_SECURITYGROUP_CLONE_CLONESECURITYGROUP_CONTROLLER, [
    AMAZON_SECURITYGROUP_CONFIGURE_CONFIGSECURITYGROUP_MIXIN_CONTROLLER,
  ])
  .controller('awsCloneSecurityGroupController', [
    '$scope',
    '$uibModalInstance',
    '$controller',
    'securityGroup',
    'application',
    function($scope, $uibModalInstance, $controller, securityGroup, application) {
      const vm = this;

      vm.firewallLabel = FirewallLabels.get('Firewall');

      $scope.pages = {
        location: require('../configure/createSecurityGroupProperties.html'),
        ingress: require('../configure/createSecurityGroupIngress.html'),
      };

      securityGroup.credentials = securityGroup.accountName;
      $scope.namePreview = securityGroup.name;

      angular.extend(
        this,
        $controller('awsConfigSecurityGroupMixin', {
          $scope: $scope,
          $uibModalInstance: $uibModalInstance,
          application: application,
          securityGroup: securityGroup,
        }),
      );

      // We want to let people clone as a means to copy security groups across
      // regions so don't block them because the names already exist.
      $scope.allowDuplicateNames = true;

      AccountService.listAccounts('aws').then(function(accounts) {
        $scope.accounts = accounts;
        vm.accountUpdated();
      });

      securityGroup.securityGroupIngress = _.chain(securityGroup.inboundRules)
        .filter(function(rule) {
          return rule.securityGroup;
        })
        .map(function(rule) {
          return rule.portRanges.map(function(portRange) {
            return {
              name: rule.securityGroup.name,
              type: rule.protocol,
              startPort: portRange.startPort,
              endPort: portRange.endPort,
            };
          });
        })
        .flatten()
        .value();

      securityGroup.ipIngress = _.chain(securityGroup.inboundRules)
        .filter(function(rule) {
          return rule.range;
        })
        .map(function(rule) {
          return rule.portRanges.map(function(portRange) {
            return {
              cidr: rule.range.ip + rule.range.cidr,
              type: rule.protocol,
              startPort: portRange.startPort,
              endPort: portRange.endPort,
            };
          });
        })
        .flatten()
        .value();

      vm.upsert = function() {
        // <account-select-field> only updates securityGroup.credentials, but Orca looks at account* before looking at credentials
        // Updating the rest of the attributes to send the correct (expected) account for all attributes
        const { credentials } = $scope.securityGroup;
        Object.assign($scope.securityGroup, {
          account: credentials,
          accountName: credentials,
          accountId: credentials,
        });

        vm.mixinUpsert('Clone');
      };

      vm.initializeSecurityGroups();
    },
  ]);
<commit_msg>fix(aws): Fix to set right state while cloning security group<commit_after>'use strict';

import * as angular from 'angular';
import _ from 'lodash';

import { AccountService, FirewallLabels } from '@spinnaker/core';
import { AMAZON_SECURITYGROUP_CONFIGURE_CONFIGSECURITYGROUP_MIXIN_CONTROLLER } from '../configure/configSecurityGroup.mixin.controller';

export const AMAZON_SECURITYGROUP_CLONE_CLONESECURITYGROUP_CONTROLLER =
  'spinnaker.amazon.securityGroup.clone.controller';
export const name = AMAZON_SECURITYGROUP_CLONE_CLONESECURITYGROUP_CONTROLLER; // for backwards compatibility
angular
  .module(AMAZON_SECURITYGROUP_CLONE_CLONESECURITYGROUP_CONTROLLER, [
    AMAZON_SECURITYGROUP_CONFIGURE_CONFIGSECURITYGROUP_MIXIN_CONTROLLER,
  ])
  .controller('awsCloneSecurityGroupController', [
    '$scope',
    '$uibModalInstance',
    '$controller',
    'securityGroup',
    'application',
    function($scope, $uibModalInstance, $controller, securityGroup, application) {
      const vm = this;

      vm.firewallLabel = FirewallLabels.get('Firewall');

      $scope.pages = {
        location: require('../configure/createSecurityGroupProperties.html'),
        ingress: require('../configure/createSecurityGroupIngress.html'),
      };

      securityGroup.credentials = securityGroup.accountName;
      $scope.namePreview = securityGroup.name;

      angular.extend(
        this,
        $controller('awsConfigSecurityGroupMixin', {
          $scope: $scope,
          $uibModalInstance: $uibModalInstance,
          application: application,
          securityGroup: securityGroup,
        }),
      );
      $scope.state.isNew = true;
      // We want to let people clone as a means to copy security groups across
      // regions so don't block them because the names already exist.
      $scope.allowDuplicateNames = true;

      AccountService.listAccounts('aws').then(function(accounts) {
        $scope.accounts = accounts;
        vm.accountUpdated();
      });

      securityGroup.securityGroupIngress = _.chain(securityGroup.inboundRules)
        .filter(function(rule) {
          return rule.securityGroup;
        })
        .map(function(rule) {
          return rule.portRanges.map(function(portRange) {
            return {
              name: rule.securityGroup.name,
              type: rule.protocol,
              startPort: portRange.startPort,
              endPort: portRange.endPort,
            };
          });
        })
        .flatten()
        .value();

      securityGroup.ipIngress = _.chain(securityGroup.inboundRules)
        .filter(function(rule) {
          return rule.range;
        })
        .map(function(rule) {
          return rule.portRanges.map(function(portRange) {
            return {
              cidr: rule.range.ip + rule.range.cidr,
              type: rule.protocol,
              startPort: portRange.startPort,
              endPort: portRange.endPort,
            };
          });
        })
        .flatten()
        .value();

      vm.upsert = function() {
        // <account-select-field> only updates securityGroup.credentials, but Orca looks at account* before looking at credentials
        // Updating the rest of the attributes to send the correct (expected) account for all attributes
        const { credentials } = $scope.securityGroup;
        Object.assign($scope.securityGroup, {
          account: credentials,
          accountName: credentials,
          accountId: credentials,
        });

        vm.mixinUpsert('Clone');
      };

      vm.initializeSecurityGroups();
    },
  ]);

{'fraction_non_alphanumeric': 0.10180995475113122, 'fraction_numerical': 0.0, 'mean_word_length': 2.5650201612903225, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6544778', 'n_tokens_mistral': 1955, 'n_tokens_neox': 1936, 'n_words': 371}