pcm-coordinator/adaptor/pcm_slurm/service/tianhe/account.go

83 lines
2.2 KiB
Go

package tianhe
/*
#cgo LDFLAGS: -lslurmdb
#include <stdio.h>
#include <slurm/slurm.h>
#include <slurm/slurmdb.h>
#include <memory.h>
#include <malloc.h>
typedef struct account_info_msg {
uint32_t record_count;
slurmdb_account_rec_t *account_array;
} account_info_msg_t;
typedef struct slurmdb_account_rec{
List assoc_list;
List coord_accts;
char *description;
char *name;
char *organization;
} slurmdb_account_rec_pcm;
struct account_info_msg get_account_info() {
struct account_info_msg accountinfo;
List accountList = NULL;
slurmdb_account_cond_t *account_cond = NULL;
void *db_conn;
db_conn = slurmdb_connection_get();
accountList = slurmdb_accounts_get(db_conn, account_cond);
slurmdb_connection_close(&db_conn);
slurmdb_account_rec_t *rec = NULL;
ListIterator itr = slurm_list_iterator_create(accountList);
int i = 0;
uint32_t length;
length = slurm_list_count(accountList);
accountinfo.record_count = length;
accountinfo.account_array = malloc(length * sizeof(slurmdb_account_rec_t));
while ((rec = slurm_list_next(itr))) {
accountinfo.account_array[i] = *rec;
i++;
}
return accountinfo;
}
struct slurmdb_account_rec *account_from_list(struct account_info_msg *list, int i) {
return (struct slurmdb_account_rec *) &list->account_array[i];
}
*/
import "C"
import (
pbslurm "code.gitlink.org.cn/JCCE/PCM.git/adaptor/pcm_slurm/gen/idl"
)
type AcctInfoMsg struct {
LastUpdate int64
RecordCount uint32
AcctInfoList []pbslurm.AccountInfo
}
func AcctDescriptorConvertCToGo(cStruct *C.struct_slurmdb_account_rec) pbslurm.AccountInfo {
var goStruct pbslurm.AccountInfo
goStruct.Name = C.GoString(cStruct.name)
return goStruct
}
func GetAcctInfo() AcctInfoMsg {
var goAcctBuffer AcctInfoMsg
cAcctBuffer := C.get_account_info()
goAcctBuffer.RecordCount = uint32(cAcctBuffer.record_count)
goAcctBuffer.AcctInfoList = make([]pbslurm.AccountInfo, cAcctBuffer.record_count, cAcctBuffer.record_count)
for i := uint32(0); i < goAcctBuffer.RecordCount; i++ {
Acct := C.account_from_list(&cAcctBuffer, C.int(i))
goAcct := AcctDescriptorConvertCToGo(Acct)
goAcctBuffer.AcctInfoList[i] = goAcct
}
return goAcctBuffer
}