func GetOrganization()

in internal/google/org.go [32:87]


func GetOrganization(domain string) (*Organization, error) {
	fmt.Println("\nGetting organization details...")
	ctx := context.Background()

	orgList := []Organization{}

	c, err := resourcemanager.NewOrganizationsClient(ctx)

	if err != nil {
		return nil, err
	}

	defer c.Close()

	req := &resourcemanagerpb.SearchOrganizationsRequest{
		Query: fmt.Sprintf(`domain:%s`, domain),
	}

	it := c.SearchOrganizations(ctx, req)
	for {
		o := Organization{}

		resp, err := it.Next()
		if err == iterator.Done {
			break
		}
		if err != nil {
			return nil, err
		}

		o.Domain = domain

		oid := strings.Split(resp.Name, "/")
		orgId, err := strconv.Atoi(oid[1])

		if err != nil {
			return nil, err
		}

		o.Id = orgId
		o.CustomerId = resp.GetDirectoryCustomerId()

		orgList = append(orgList, o)
	}

	// Check if multiple orgs (or no orgs) were found
	if len(orgList) > 1 {
		err := errors.New("too many orgs found - multiple orgs not supported")
		return nil, err
	} else if len(orgList) == 0 {
		err := errors.New("no org found")
		return nil, err
	} else {
		return &orgList[0], err
	}
}