# Add new Load Balancer Link via API

**URL:** <https://forums.suse.com/t/add-new-load-balancer-link-via-api/137>\
**Category:** Rancher 1.x\
**Created:** [July 21, 2015, 8:39pm UTC](https://forums.suse.com/t/add-new-load-balancer-link-via-api/137 "2015-07-21T20:39:40Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![jwhitcraft](https://sea2.discourse-cdn.com/flex022/user_avatar/forums.suse.com/jwhitcraft/32/37_2.png) [@jwhitcraft](https://forums.suse.com/u/jwhitcraft)\
**Post date:** [July 21, 2015, 8:39pm UTC](https://forums.suse.com/t/add-new-load-balancer-link-via-api/137/1 "2015-07-21T20:39:40Z")

</div>

Hello,

I’m trying to programmatically add a new hostname target to a load balancer, but i can’t seem to get it working with.

I’m using this:

> curl -u “${CATTLE\_ACCESS\_KEY}:${CATTLE\_SECRET\_KEY}”   
> -X POST   
> -H ‘Accept: application/json’   
> -H ‘Content-Type: application/json’   
> -d ‘{“serviceLink”:{“serviceId”:“1s17”, “ports”:[“80:rubytest.test.dev”]}}’   
> ‘http://${CATTLE\_HOST}/v1/projects/1a5/loadbalancerservices/1s18/?action=addservicelink’

any help would be appreciated.

---

<div class="post-metadata">

**Author:** ![vincent](https://sea2.discourse-cdn.com/flex022/user_avatar/forums.suse.com/vincent/32/7156_2.png) [@vincent](https://forums.suse.com/u/vincent)\
**Post date:** [July 21, 2015, 8:53pm UTC](https://forums.suse.com/t/add-new-load-balancer-link-via-api/137/2 "2015-07-21T20:53:42Z")

</div>

@jwhitcraft What Rancher version (click the cow)? Is there a specific error message/code you’re getting back? I tried copying & pasting your command + substituting the relevant IDs and vars and it worked for me.

---

<div class="post-metadata">

**Author:** ![jwhitcraft](https://sea2.discourse-cdn.com/flex022/user_avatar/forums.suse.com/jwhitcraft/32/37_2.png) [@jwhitcraft](https://forums.suse.com/u/jwhitcraft)\
**Post date:** [July 21, 2015, 9:08pm UTC](https://forums.suse.com/t/add-new-load-balancer-link-via-api/137/3 "2015-07-21T21:08:31Z")

</div>

Using v0.28, and all i get back is the page from http://${CATTLE\_HOST}/v1/projects/1a5/loadbalancerservices/1s18/ when i view it in a browser.

---

<div class="post-metadata">

**Author:** ![jwhitcraft](https://sea2.discourse-cdn.com/flex022/user_avatar/forums.suse.com/jwhitcraft/32/37_2.png) [@jwhitcraft](https://forums.suse.com/u/jwhitcraft)\
**Post date:** [July 21, 2015, 9:36pm UTC](https://forums.suse.com/t/add-new-load-balancer-link-via-api/137/4 "2015-07-21T21:36:31Z")

</div>

The way i test to see if it got added is to go into rancher and edit the Load Balancer Service, but I’m not seeing anything new besides the one i put in there via the api.

Is there a way to get the targets via the api? I’m not seeing it.

Thanks.

---

<div class="post-metadata">

**Author:** ![vincent](https://sea2.discourse-cdn.com/flex022/user_avatar/forums.suse.com/vincent/32/7156_2.png) [@vincent](https://forums.suse.com/u/vincent)\
**Post date:** [July 21, 2015, 9:47pm UTC](https://forums.suse.com/t/add-new-load-balancer-link-via-api/137/5 "2015-07-21T21:47:01Z")

</div>

The normal response of adding a link is the `loadBalancerService` itself, so that should mean you’re getting back a `200` and it was successful. If you’re watching the UI at the same time the link should show up.

There is a `consumedservices` link (as in “in the links section of the loadBalancerService resource”) that will tell you the services that are linked but that returns the actual services so it doesn’t include the port mappings. To get those you currently have to go the other way, `/v1/projects/1a5/serviceconsumemaps?serviceId=1s18`.

The UI and rancher-compose are 100% API-driven, so if they can do it there’s a way to do it in the API… This would be an example where it’s not necessarily discoverable though.

---

<div class="post-metadata">

**Author:** ![jwhitcraft](https://sea2.discourse-cdn.com/flex022/user_avatar/forums.suse.com/jwhitcraft/32/37_2.png) [@jwhitcraft](https://forums.suse.com/u/jwhitcraft)\
**Post date:** [July 21, 2015, 11:09pm UTC](https://forums.suse.com/t/add-new-load-balancer-link-via-api/137/6 "2015-07-21T23:09:27Z")

</div>

@vincent, I figured out the problem. since i already had another mapping to that server, the addservicelink didn’t work. Once i changed to a different serviceId, the link was added.

generally, we won’t have more than one link from a lb to a service, but it’s good to know what I was doing wrong. Maybe a bug should be field for use case to kick back an error or maybe append to it, if it’s already there?

what are your thoughts?

---

<div class="post-metadata">

**Author:** ![vincent](https://sea2.discourse-cdn.com/flex022/user_avatar/forums.suse.com/vincent/32/7156_2.png) [@vincent](https://forums.suse.com/u/vincent)\
**Post date:** [July 22, 2015, 5:53am UTC](https://forums.suse.com/t/add-new-load-balancer-link-via-api/137/7 "2015-07-22T05:53:56Z")

</div>

Ah, that makes sense… ports were added more recently so it’s a bug in `addServiceLink` to not handle the new ports or at least say 422 because that service already exists.

I don’t know of an issue for that if you’d like to add one. The UI actually only uses the `set` version. We’ve removed `add`/`removeThing` actions in some other places because they’re not idempotent. So our resolution might just be to drop those actions… See how `set` works for you?

---

<div class="post-metadata">

**Author:** ![jwhitcraft](https://sea2.discourse-cdn.com/flex022/user_avatar/forums.suse.com/jwhitcraft/32/37_2.png) [@jwhitcraft](https://forums.suse.com/u/jwhitcraft)\
**Post date:** [July 22, 2015, 11:47am UTC](https://forums.suse.com/t/add-new-load-balancer-link-via-api/137/8 "2015-07-22T11:47:09Z")

</div>

My problem with set is, I would have to pull down all the links first just to add one. Like i said, I shouldn’t have more than one link per service so the add will work fine my use case.

---

<div class="post-metadata">

**Author:** ![vincent](https://sea2.discourse-cdn.com/flex022/user_avatar/forums.suse.com/vincent/32/7156_2.png) [@vincent](https://forums.suse.com/u/vincent)\
**Post date:** [July 22, 2015, 6:29pm UTC](https://forums.suse.com/t/add-new-load-balancer-link-via-api/137/9 "2015-07-22T18:29:05Z")

</div>

Ok; @alena can we make `addServiceLink` and `removeServiceLink` handle new ports? Something like `add` would add the given ports if there is an array and `remove` would remove only the specified port if an array is given, otherwise the whole service.

---

<div class="post-metadata">

**Author:** ![alena](https://sea2.discourse-cdn.com/flex022/user_avatar/forums.suse.com/alena/32/50_2.png) [@alena](https://forums.suse.com/u/alena)\
**Post date:** [July 22, 2015, 7:31pm UTC](https://forums.suse.com/t/add-new-load-balancer-link-via-api/137/10 "2015-07-22T19:31:18Z")

</div>

@vincent @jwhitcraft Sure, will be fixed. Here is the ticket to keep track of the issue: [https://github.com/rancher/rancher/issues/1616](https://github.com/rancher/rancher/issues/1616)
