func()

in providers/ibm/ibm_is_virtual_endpoint_gateway.go [54:124]


func (g *VPEGenerator) InitResources() error {
	region := g.Args["region"].(string)
	apiKey := os.Getenv("IC_API_KEY")
	if apiKey == "" {
		return fmt.Errorf("No API key set")
	}

	isURL := GetVPCEndPoint(region)
	iamURL := GetAuthEndPoint()
	vpcoptions := &vpcv1.VpcV1Options{
		URL: isURL,
		Authenticator: &core.IamAuthenticator{
			ApiKey: apiKey,
			URL:    iamURL,
		},
	}
	vpcclient, err := vpcv1.NewVpcV1(vpcoptions)
	if err != nil {
		return err
	}

	start := ""
	allrecs := []vpcv1.EndpointGateway{}
	for {
		listEndpointGatewaysOptions := &vpcv1.ListEndpointGatewaysOptions{}
		if start != "" {
			listEndpointGatewaysOptions.Start = &start
		}
		if rg := g.Args["resource_group"].(string); rg != "" {
			rg, err = GetResourceGroupID(apiKey, rg, region)
			if err != nil {
				return fmt.Errorf("Error Fetching Resource Group Id %s", err)
			}
			listEndpointGatewaysOptions.ResourceGroupID = &rg
		}
		gateways, response, err := vpcclient.ListEndpointGateways(listEndpointGatewaysOptions)
		if err != nil {
			return fmt.Errorf("Error Fetching endpoint gateways %s\n%s", err, response)
		}
		start = GetNext(gateways.Next)
		allrecs = append(allrecs, gateways.EndpointGateways...)
		if start == "" {
			break
		}
	}

	for _, gateway := range allrecs {
		start := ""
		allrecs := []vpcv1.ReservedIP{}
		g.Resources = append(g.Resources, g.createVPEGatewayResources(*gateway.ID, *gateway.Name))
		listEndpointGatewayIpsOptions := &vpcv1.ListEndpointGatewayIpsOptions{
			EndpointGatewayID: gateway.ID,
		}
		if start != "" {
			listEndpointGatewayIpsOptions.Start = &start
		}
		ips, response, err := vpcclient.ListEndpointGatewayIps(listEndpointGatewayIpsOptions)
		if err != nil {
			return fmt.Errorf("Error Fetching endpoint gateway ips %s\n%s", err, response)
		}
		start = GetNext(ips.Next)
		allrecs = append(allrecs, ips.Ips...)
		if start == "" {
			break
		}
		for _, ip := range allrecs {
			g.Resources = append(g.Resources, g.createVPEGatewayIPResources(*gateway.ID, *ip.ID, *ip.Name))
		}
	}
	return nil
}