func()

in providers/ibm/satellite_data_plane.go [186:377]


func (g *SatelliteDataPlaneGenerator) InitResources() error {
	vpcName := g.Args["vpc"].(string)
	if len(vpcName) == 0 {
		return fmt.Errorf("required VPC name missing, '-vpc=<vpcName>' flag not set")
	}

	region := g.Args["region"].(string)
	bmxConfig := &bluemix.Config{
		BluemixAPIKey: os.Getenv("IC_API_KEY"),
	}

	sess, err := session.New(bmxConfig)
	if err != nil {
		return err
	}

	err = authenticateAPIKey(sess)
	if err != nil {
		return err
	}

	// VPC
	vpcObj, err := vpcClient(region, sess)
	if err != nil {
		log.Println("Error building VPC object: ", err)
		return err
	}

	start := ""
	allVPCrecs := []vpcv1.VPC{}
	for {
		listVpcsOptions := &vpcv1.ListVpcsOptions{}
		if start != "" {
			listVpcsOptions.Start = &start
		}
		vpcs, response, err := vpcObj.ListVpcs(listVpcsOptions)
		if err != nil {
			return fmt.Errorf("Error Fetching vpcs %s\n%s", err, response)
		}

		start = GetNext(vpcs.Next)
		allVPCrecs = append(allVPCrecs, vpcs.Vpcs...)
		if start == "" {
			break
		}
	}

	// VPC & Instances
	for _, vpc := range allVPCrecs {
		if *vpc.Name == vpcName {

			var vpcDependsOn []string
			vpcDependsOn = append(vpcDependsOn,
				"ibm_is_vpc."+terraformutils.TfSanitize(*vpc.Name))

			g.Resources = append(g.Resources, g.loadVPCResources(*vpc.ID, *vpc.Name))

			start = ""
			var allrecs []vpcv1.Instance
			for {
				options := &vpcv1.ListInstancesOptions{}
				if start != "" {
					options.Start = &start
				}

				instances, response, err := vpcObj.ListInstances(options)
				if err != nil {
					return fmt.Errorf("Error Fetching Instances %s\n%s", err, response)
				}
				start = GetNext(instances.Next)
				allrecs = append(allrecs, instances.Instances...)
				if start == "" {
					break
				}
			}

			// Floating IP
			start := ""
			allFloatingIPs := []vpcv1.FloatingIP{}
			for {
				floatingIPOptions := &vpcv1.ListFloatingIpsOptions{}
				if start != "" {
					floatingIPOptions.Start = &start
				}
				floatingIPs, response, err := vpcObj.ListFloatingIps(floatingIPOptions)
				if err != nil {
					return fmt.Errorf("Error Fetching floating IPs %s\n%s", err, response)
				}
				start = GetNext(floatingIPs.Next)
				allFloatingIPs = append(allFloatingIPs, floatingIPs.FloatingIps...)
				if start == "" {
					break
				}
			}

			for _, instance := range allrecs {
				g.Resources = append(g.Resources, g.loadInstanceResources(instance, vpcDependsOn))

				for _, ip := range allFloatingIPs {
					target, _ := ip.Target.(*vpcv1.FloatingIPTarget)
					if *target.ID == *instance.PrimaryNetworkInterface.ID {
						g.Resources = append(g.Resources, g.loadFloatingIPResources(*ip.ID, *ip.Name))
					}
				}
			}

			// Security group
			start = ""
			var allSgRecs []vpcv1.SecurityGroup
			for {
				options := &vpcv1.ListSecurityGroupsOptions{
					VPCID: vpc.ID,
				}
				if start != "" {
					options.Start = &start
				}

				sgs, response, err := vpcObj.ListSecurityGroups(options)
				if err != nil {
					return fmt.Errorf("Error Fetching security Groups %s\n%s", err, response)
				}
				start = GetNext(sgs.Next)
				allSgRecs = append(allSgRecs, sgs.SecurityGroups...)
				if start == "" {
					break
				}
			}

			for _, group := range allSgRecs {
				var sgDependsOn []string
				sgDependsOn = append(sgDependsOn,
					"ibm_is_security_group."+terraformutils.TfSanitize(*group.Name))
				g.Resources = append(g.Resources, g.loadSecurityGroupResources(*group.ID, *group.Name))
				listSecurityGroupRulesOptions := &vpcv1.ListSecurityGroupRulesOptions{
					SecurityGroupID: group.ID,
				}
				rules, response, err := vpcObj.ListSecurityGroupRules(listSecurityGroupRulesOptions)
				if err != nil {
					return fmt.Errorf("Error Fetching security group rules %s\n%s", err, response)
				}
				for _, sgrule := range rules.Rules {
					switch reflect.TypeOf(sgrule).String() {
					case "*vpcv1.SecurityGroupRuleSecurityGroupRuleProtocolIcmp":
						{
							rule := sgrule.(*vpcv1.SecurityGroupRuleSecurityGroupRuleProtocolIcmp)
							g.Resources = append(g.Resources, g.loadSecurityGroupRuleResources(*group.ID, *rule.ID, sgDependsOn))
						}

					case "*vpcv1.SecurityGroupRuleSecurityGroupRuleProtocolAll":
						{
							rule := sgrule.(*vpcv1.SecurityGroupRuleSecurityGroupRuleProtocolAll)
							g.Resources = append(g.Resources, g.loadSecurityGroupRuleResources(*group.ID, *rule.ID, sgDependsOn))
						}

					case "*vpcv1.SecurityGroupRuleSecurityGroupRuleProtocolTcpudp":
						{
							rule := sgrule.(*vpcv1.SecurityGroupRuleSecurityGroupRuleProtocolTcpudp)
							g.Resources = append(g.Resources, g.loadSecurityGroupRuleResources(*group.ID, *rule.ID, sgDependsOn))
						}
					}
				}
			}

			// Subnet
			start = ""
			var allSubNetRecs []vpcv1.Subnet
			for {
				options := &vpcv1.ListSubnetsOptions{}
				if start != "" {
					options.Start = &start
				}

				subnets, response, err := vpcObj.ListSubnets(options)
				if err != nil {
					return fmt.Errorf("Error Fetching subnets %s\n%s", err, response)
				}
				start = GetNext(subnets.Next)
				allSubNetRecs = append(allSubNetRecs, subnets.Subnets...)
				if start == "" {
					break
				}
			}

			for _, subnet := range allSubNetRecs {
				if *subnet.VPC.Name == vpcName {
					g.Resources = append(g.Resources, g.loadSubnetResources(*subnet.ID, *subnet.Name, vpcDependsOn))
				}
			}
		}
	}
	return nil
}