def Run(self, args):
        holder = base_classes.ComputeApiHolder(self.ReleaseTrack())
        client = holder.client

        network_ref = flags.NetworkArgument().ResolveAsResource(
            args,
            holder.resources,
            scope_lister=compute_flags.GetDefaultScopeLister(client))

        request = client.messages.ComputeNetworksGetEffectiveFirewallsRequest(
            **network_ref.AsDict())
        responses = client.MakeRequests([(client.apitools_client.networks,
                                          'GetEffectiveFirewalls', request)])
        res = responses[0]
        org_firewall = []
        network_firewall = []
        all_firewall_policy = []

        if hasattr(res, 'firewalls'):
            network_firewall = firewalls_utils.SortNetworkFirewallRules(
                client, res.firewalls)

        if hasattr(res, 'firewallPolicys') and res.firewallPolicys:
            for fp in res.firewallPolicys:
                firewall_policy_rule = firewalls_utils.SortFirewallPolicyRules(
                    client, fp.rules)
                fp_response = (
                    client.messages.
                    NetworksGetEffectiveFirewallsResponseEffectiveFirewallPolicy(
                        name=fp.name, rules=firewall_policy_rule,
                        type=fp.type))
                all_firewall_policy.append(fp_response)
        elif hasattr(res, 'organizationFirewalls'):
            for sp in res.organizationFirewalls:
                org_firewall_rule = firewalls_utils.SortOrgFirewallRules(
                    client, sp.rules)
                org_firewall.append(
                    client.messages.
                    NetworksGetEffectiveFirewallsResponseOrganizationFirewallPolicy(
                        id=sp.id, rules=org_firewall_rule))

        if args.IsSpecified('format') and args.format == 'json':
            if org_firewall:
                return client.messages.NetworksGetEffectiveFirewallsResponse(
                    organizationFirewalls=org_firewall,
                    firewalls=network_firewall,
                    firewallPolicys=all_firewall_policy)
            else:
                return client.messages.NetworksGetEffectiveFirewallsResponse(
                    firewalls=network_firewall,
                    firewallPolicys=all_firewall_policy)

        result = []
        for fp in all_firewall_policy:
            result.extend(
                firewalls_utils.ConvertFirewallPolicyRulesToEffectiveFwRules(
                    client, fp, True))
        for sp in org_firewall:
            result.extend(
                firewalls_utils.
                ConvertOrgSecurityPolicyRulesToEffectiveFwRules(sp))
        result.extend(
            firewalls_utils.ConvertNetworkFirewallRulesToEffectiveFwRules(
                network_firewall))
        return result
Example #2
0
  def Run(self, args):
    holder = base_classes.ComputeApiHolder(self.ReleaseTrack())
    client = holder.client
    messages = holder.client.messages

    instance_ref = instances_flags.INSTANCE_ARG.ResolveAsResource(
        args,
        holder.resources,
        scope_lister=flags.GetDefaultScopeLister(holder.client))

    instance = client.apitools_client.instances.Get(
        messages.ComputeInstancesGetRequest(**instance_ref.AsDict()))
    for i in instance.networkInterfaces:
      if i.name == args.network_interface:
        break
    else:
      raise exceptions.UnknownArgumentException(
          'network-interface',
          'Instance does not have a network interface [{}], '
          'present interfaces are [{}].'.format(
              args.network_interface,
              ', '.join([i.name for i in instance.networkInterfaces])))

    request = messages.ComputeInstancesGetEffectiveFirewallsRequest(
        project=instance_ref.project,
        instance=instance_ref.instance,
        zone=instance_ref.zone,
        networkInterface=args.network_interface)
    res = client.apitools_client.instances.GetEffectiveFirewalls(request)
    org_firewall = []
    network_firewall = []
    org_firewall_policy = []
    all_firewall_policy = []

    if hasattr(res, 'firewalls'):
      network_firewall = firewalls_utils.SortNetworkFirewallRules(
          client, res.firewalls)

    if hasattr(res, 'firewallPolicys') and res.firewallPolicys:
      for fp in res.firewallPolicys:
        firewall_policy_rule = firewalls_utils.SortFirewallPolicyRules(
            client, fp.rules)
        if (fp.type == client.messages
            .InstancesGetEffectiveFirewallsResponseEffectiveFirewallPolicy
            .TypeValueValuesEnum.HIERARCHY):
          fp_response = (
              client.messages
              .InstancesGetEffectiveFirewallsResponseEffectiveFirewallPolicy(
                  name=fp.name, rules=firewall_policy_rule))
          org_firewall_policy.append(fp_response)
          all_firewall_policy.append(fp_response)
    elif hasattr(res, 'organizationFirewalls'):
      for sp in res.organizationFirewalls:
        org_firewall_rule = firewalls_utils.SortOrgFirewallRules(
            client, sp.rules)
        org_firewall.append(
            client.messages
            .InstancesGetEffectiveFirewallsResponseOrganizationFirewallPolicy(
                id=sp.id, rules=org_firewall_rule))

    if args.IsSpecified('format') and args.format == 'json':
      return client.messages.InstancesGetEffectiveFirewallsResponse(
          organizationFirewalls=org_firewall,
          firewalls=network_firewall,
          firewallPolicys=all_firewall_policy)

    result = []
    for fp in org_firewall_policy:
      result.extend(
          firewalls_utils.ConvertFirewallPolicyRulesToEffectiveFwRules(fp))
    for sp in org_firewall:
      result.extend(
          firewalls_utils.ConvertOrgSecurityPolicyRulesToEffectiveFwRules(sp))
    result.extend(
        firewalls_utils.ConvertNetworkFirewallRulesToEffectiveFwRules(
            network_firewall))
    return result
Example #3
0
    def Run(self, args):
        holder = base_classes.ComputeApiHolder(self.ReleaseTrack())
        client = holder.client
        messages = holder.client.messages

        if hasattr(args, 'project') and args.project:
            project = args.project
        else:
            project = properties.VALUES.core.project.GetOrFail()

        if hasattr(args, 'region') and args.region:
            region = args.region
        else:
            region = properties.VALUES.compute.region.GetOrFail()

        network = network_flags.NetworkArgumentForOtherResource(
            short_help=None).ResolveAsResource(args, holder.resources)
        network_ref = network.SelfLink() if network else None

        request = messages.ComputeRegionNetworkFirewallPoliciesGetEffectiveFirewallsRequest(
            project=project, region=region, network=network_ref)

        responses = client.MakeRequests([
            (client.apitools_client.regionNetworkFirewallPolicies,
             'GetEffectiveFirewalls', request)
        ])
        res = responses[0]
        network_firewall = []
        all_firewall_policy = []

        if hasattr(res, 'firewalls'):
            network_firewall = firewalls_utils.SortNetworkFirewallRules(
                client, res.firewalls)

        if hasattr(res, 'firewallPolicys') and res.firewallPolicys:
            for fp in res.firewallPolicys:
                firewall_policy_rule = firewalls_utils.SortFirewallPolicyRules(
                    client, fp.rules)
                fp_response = (
                    client.messages.
                    RegionNetworkFirewallPoliciesGetEffectiveFirewallsResponseEffectiveFirewallPolicy(
                        name=fp.name, rules=firewall_policy_rule,
                        type=fp.type))
                all_firewall_policy.append(fp_response)

        if args.IsSpecified('format') and args.format == 'json':
            return client.messages.RegionNetworkFirewallPoliciesGetEffectiveFirewallsResponse(
                firewalls=network_firewall,
                firewallPolicys=all_firewall_policy)

        result = []
        for fp in all_firewall_policy:
            result.extend(
                firewalls_utils.ConvertFirewallPolicyRulesToEffectiveFwRules(
                    client,
                    fp,
                    True,
                    support_region_network_firewall_policy=True))
        result.extend(
            firewalls_utils.ConvertNetworkFirewallRulesToEffectiveFwRules(
                network_firewall))
        return result