'Add an iSCSI initiator to initiatorgroup'

def add_to_initiatorgroup(self, initiator, initiatorgroup):

    svc = ('/api/san/v1/iscsi/initiator-groups/' + initiatorgroup)
    ret = self.rclient.get(svc)
    if (ret.status != restclient.Status.OK):
        svc = '/api/san/v1/iscsi/initiator-groups'
        arg = {'name': initiatorgroup, 'initiators': [initiator]}
        ret = self.rclient.post(svc, arg)
        if (ret.status != restclient.Status.CREATED):
            exception_msg = (_('Error    Adding    Initiator:    %(initiator)s    on    groupInitiatorGroup:    %(initiatorgroup)s    Return    code:    %(ret.status)d    Message:    %(ret.data)s    .') % {'initiator': initiator, 'initiatorgroup': initiatorgroup, 'ret.status': ret.status, 'ret.data': ret.data})
            LOG.error(exception_msg)
            raise exception.VolumeBackendAPIException(data=exception_msg)
    else:
        val = json.loads(ret.data)
        inits = val['group']['initiators']
        if (inits is None):
            exception_msg = (_('Error    Getting    Initiators:    InitiatorGroup:    %(initiatorgroup)s    Return    code:    %(ret.status)d    Message:    %(ret.data)s    .') % {'initiatorgroup': initiatorgroup, 'ret.status': ret.status, 'ret.data': ret.data})
            LOG.error(exception_msg)
            raise exception.VolumeBackendAPIException(data=exception_msg)
        if (initiator in inits):
            return
        inits.append(initiator)
        svc = ('/api/san/v1/iscsi/initiator-groups/' + initiatorgroup)
        arg = {'initiators': inits}
        ret = self.rclient.put(svc, arg)
        if (ret.status != restclient.Status.ACCEPTED):
            exception_msg = (_('Error    Adding    Initiator:    %(initiator)s    on    groupInitiatorGroup:    %(initiatorgroup)s    Return    code:    %(ret.status)d    Message:    %(ret.data)s    .') % {'initiator': initiator, 'initiatorgroup': initiatorgroup, 'ret.status': ret.status, 'ret.data': ret.data})
            LOG.error(exception_msg)
            raise exception.VolumeBackendAPIException(data=exception_msg)
